It is currently October 14th, 2024, 7:59 pm

Plugin DriveInfo

Share and get help with Plugins and Addons
User avatar
death.crafter
Rainmeter Sage
Posts: 1398
Joined: April 24th, 2021, 8:13 pm

Plugin DriveInfo

Post by death.crafter »

A little plugin to get drive info of drives.

Options:
  • Drive : The drive letter we want information of, e.g. C, D etc.
  • Type : The information we want. Valid values are:
    • DriveType
    • FileSystem (NTFS, FAT32 etc)
    • FreeSpace
    • Label
    • TotalSpace

Inline Functions:
  • GetDriveType(driveletter) : [&MeasureName:GetDriveType(C)] = Fixed
  • GetFileSystem(driveletter) : [&MeasureName:GetFileSystem(C)] = NTFS
  • GetFreeSpace(driveletter) : [&MeasureName:GetFreeSpace(C)] = 110595407872
  • GetLabel(driveletter) : [&MeasureName:GetLabel(D)] = OS
  • GetTotalSpace(driveletter) : [&MeasureName:GetTotalSpace(C)] = 246960619520

Usage:

Code: Select all

[DriveInfo]
Measure=Plugin
Plugin=DriveInfo
Drive=D
Type=Label

Source(C#):

Code: Select all

using Rainmeter;
using System;
using System.Runtime.InteropServices;

namespace DriveInfo
{
    class Measure
    {
        static public implicit operator Measure(IntPtr data)
        {
            return (Measure)GCHandle.FromIntPtr(data).Target;
        }
        public IntPtr buffer = IntPtr.Zero;

        public API rmapi;

        public string drive;

        public string type;

        public bool driveValid = false;

        public System.IO.DriveInfo driveInfo;
    }

    public class Plugin
    {
        [DllExport]
        public static void Initialize(ref IntPtr data, IntPtr rm)
        {
            data = GCHandle.ToIntPtr(GCHandle.Alloc(new Measure()));
            Rainmeter.API api = (Rainmeter.API)rm;
        }

        [DllExport]
        public static void Finalize(IntPtr data)
        {
            Measure measure = (Measure)data;
            if (measure.buffer != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(measure.buffer);
            }
            GCHandle.FromIntPtr(data).Free();
        }

        [DllExport]
        public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
        {
            Measure measure = (Measure)data;

            measure.rmapi = new Rainmeter.API(rm);

            measure.drive = measure.rmapi.ReadString("Drive", "");

            measure.type = measure.rmapi.ReadString("Type", "drivetype").ToLower();

            if (String.IsNullOrEmpty(measure.drive)) return;

            try
            {
                measure.driveInfo = new System.IO.DriveInfo(measure.drive.Substring(0, 1));
                measure.driveValid = true;
            }
            catch
            {
                measure.rmapi.Log(API.LogType.Error, String.Format("Drive {0} doesn't exist.", measure.drive));
            }
        }

        [DllExport]
        public static double Update(IntPtr data)
        {
            Measure measure = (Measure)data;

            if (!measure.driveValid)
                return 0.0;
            if (!measure.driveInfo.IsReady)
                return 0.0;
            if (measure.type == "freespace")
                return Convert.ToDouble(GetDriveInfo(measure.driveInfo, measure.type));
            if (measure.type == "totalspace")
                return Convert.ToDouble(GetDriveInfo(measure.driveInfo, measure.type));
            return 0.0;
        }

        [DllExport]
        public static IntPtr GetString(IntPtr data)
        {
            Measure measure = (Measure)data;
            if (measure.buffer != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(measure.buffer);
                measure.buffer = IntPtr.Zero;
            }

            if (measure.driveValid)
            {
                measure.buffer = Marshal.StringToHGlobalUni(GetDriveInfo(measure.driveInfo, measure.type));
            }
            
            return measure.buffer;
        }

        public static string GetDriveInfo(System.IO.DriveInfo drive, string infoType)
        {
            if (!drive.IsReady)
            {
                if (infoType.Equals("drivetype"))
                {
                    return drive.DriveType.ToString();
                }
                else if (infoType.Equals("label"))
                {
                    switch (drive.DriveType)
                    {
                        case System.IO.DriveType.CDRom:
                            return "CD Drive";
                        case System.IO.DriveType.Fixed:
                            return "Fixed Drive";
                        case System.IO.DriveType.Network:
                            return "Network Drive";
                        case System.IO.DriveType.Ram:
                            return "RAM Disk";
                        case System.IO.DriveType.Removable:
                            return "Removable Drive";
                        default:
                            return "Unknown";
                    }
                }
                else if (infoType.Equals("filesystem"))
                {
                    return "Unidentified";
                }
                else
                {
                    return null;
                }
            }
            switch (infoType)
            {
                case "drivetype":
                    return drive.DriveType.ToString();
                case "filesystem":
                    return drive.DriveFormat;
                case "freespace":
                    return drive.AvailableFreeSpace.ToString();
                case "label":
                    return drive.VolumeLabel;
                case "totalspace":
                    return drive.TotalSize.ToString();
                default:
                    return null;
            }
        }

        public static string TrimQuotes(string rawString)
        {
            string outString = rawString;
            if ((rawString.StartsWith("\"") && rawString.EndsWith("\"")) || (rawString.StartsWith("'") && rawString.EndsWith("'")))
            {
                outString = outString.Remove(0, 1);
                outString = outString.Remove(outString.Length - 1, 1);
                return outString;
            }
            return rawString;
        }

        [DllExport]
        public static IntPtr GetLabel(IntPtr data, int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 1)] string[] argv)
        {
            Measure measure = (Measure)data;
            if (argc <= 0)
            {
                return IntPtr.Zero;
            }

            System.IO.DriveInfo drive;
            string output = null;
            try
            {
                drive = new System.IO.DriveInfo(TrimQuotes(argv[0]).Substring(0, 1));
                output = GetDriveInfo(drive, "label");
            }
            catch
            {
                return Marshal.StringToHGlobalUni(TrimQuotes(argv[0]));
            }

            if (!String.IsNullOrEmpty(output))
            {
                return Marshal.StringToHGlobalUni(output);
            }

            return Marshal.StringToHGlobalUni("Unidentified");
        }

        [DllExport]
        public static IntPtr GetFileSystem(IntPtr data, int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 1)] string[] argv)
        {
            Measure measure = (Measure)data;
            if (argc <= 0)
            {
                return IntPtr.Zero;
            }

            System.IO.DriveInfo drive;
            string output;
            try
            {
                drive = new System.IO.DriveInfo(TrimQuotes(argv[0]).Substring(0, 1));
                if (!drive.IsReady)
                {
                    return Marshal.StringToHGlobalUni("Unidentified");
                }
                else
                {
                    output = GetDriveInfo(drive, "filesystem");
                }
            }
            catch
            {
                return IntPtr.Zero;
            }

            if (!String.IsNullOrEmpty(output)) return Marshal.StringToHGlobalUni(output);

            return Marshal.StringToHGlobalUni("Unidentified");
        }

        [DllExport]
        public static IntPtr GetDriveType(IntPtr data, int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 1)] string[] argv)
        {
            Measure measure = (Measure)data;
            if (argc <= 0)
            {
                return IntPtr.Zero;
            }

            System.IO.DriveInfo drive;
            string output;
            try
            {
                drive = new System.IO.DriveInfo(TrimQuotes(argv[0]));
                output = drive.DriveType.ToString();
            }
            catch
            {
                return Marshal.StringToHGlobalUni("Unidentified");
            }

            if (!String.IsNullOrEmpty(output)) return Marshal.StringToHGlobalUni(output);

            return Marshal.StringToHGlobalUni("Unidentified");
        }

        [DllExport]
        public static IntPtr GetTotalSpace(IntPtr data, int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 1)] string[] argv)
        {
            Measure measure = (Measure)data;
            if (argc <= 0)
            {
                return IntPtr.Zero;
            }

            System.IO.DriveInfo drive;
            string output;
            try
            {
                drive = new System.IO.DriveInfo(TrimQuotes(argv[0]));
                if (!drive.IsReady)
                {
                    return Marshal.StringToHGlobalUni("0");
                }
                else
                {
                    output = GetDriveInfo(drive, "totalspace");
                }
            }
            catch
            {
                return Marshal.StringToHGlobalUni("Unidentified");
            }

            if (!String.IsNullOrEmpty(output)) return Marshal.StringToHGlobalUni(output);

            return Marshal.StringToHGlobalUni("Unidentified");
        }

        [DllExport]
        public static IntPtr GetFreeSpace(IntPtr data, int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 1)] string[] argv)
        {
            Measure measure = (Measure)data;
            if (argc <= 0)
            {
                return IntPtr.Zero;
            }

            System.IO.DriveInfo drive;
            string output;
            try
            {
                drive = new System.IO.DriveInfo(TrimQuotes(argv[0]));
                if (!drive.IsReady)
                {
                    return Marshal.StringToHGlobalUni("0");
                }
                else
                {
                    output = GetDriveInfo(drive, "freespace");
                }
            }
            catch
            {
                return Marshal.StringToHGlobalUni("Unidentified");
            }

            if (!String.IsNullOrEmpty(output)) return Marshal.StringToHGlobalUni(output);

            return Marshal.StringToHGlobalUni("Unidentified");
        }
    }
}

Example Skin:
Example skin inspired from TitanDrives by CodeCode
DriveInfo_1.0.0.1.rmskin

Preview:
Screenshot 2021-10-24 160615.png

Redistributables:
DriveInfo.zip
You do not have the required permissions to view the files attached to this post.
from the Realm of Death
User avatar
sl23
Posts: 1832
Joined: February 17th, 2011, 7:45 pm
Location: a Galaxy S7 far far away

Re: Plugin DriveInfo

Post by sl23 »

Nice skin :thumbup:
I'm curious why you made this when there is a default plugin already? Is this more efficient?
57686174 77696C6C 6265 77696C6C 6265
User avatar
death.crafter
Rainmeter Sage
Posts: 1398
Joined: April 24th, 2021, 8:13 pm

Re: Plugin DriveInfo

Post by death.crafter »

sl23 wrote: October 25th, 2021, 9:45 am Nice skin :thumbup:
I'm curious why you made this when there is a default plugin already? Is this more efficient?
Because, it has inline functions. You don't have to create 10 measures to get 10 drive infos. One is enough :Whistle

Also, I don't have this handicap,
Screenshot 2021-10-25 162305.png
and the name is more legible.
You do not have the required permissions to view the files attached to this post.
from the Realm of Death
User avatar
Active Colors
Moderator
Posts: 1319
Joined: February 16th, 2012, 3:32 am
Location: Berlin, Germany

Re: Plugin DriveInfo

Post by Active Colors »

Well done! Thank you! :welcome:
User avatar
Active Colors
Moderator
Posts: 1319
Joined: February 16th, 2012, 3:32 am
Location: Berlin, Germany

Re: Plugin DriveInfo

Post by Active Colors »

Quick questions.
I don't understand. Why do you need to extract icons?
Comparing to the example skin from the docs, your icons looks smudged, while icons in the example skin look crisp. Both, your skin and the example skin use Large IconSize.

The website preview resizes the. See the picture here to see the details:
https://forum.rainmeter.net/download/file.php?id=26077&mode=view
You do not have the required permissions to view the files attached to this post.
User avatar
death.crafter
Rainmeter Sage
Posts: 1398
Joined: April 24th, 2021, 8:13 pm

Re: Plugin DriveInfo

Post by death.crafter »

Active Colors wrote: October 25th, 2021, 11:38 am Quick questions.
I don't understand. Why do you need to extract icons?
Comparing to the example skin from the docs, your icons looks smudged, while icons in the example skin look crisp. Both, your skin and the example skin use Large IconSize.

The website preview resizes the. See the picture here to see the details:
https://forum.rainmeter.net/download/file.php?id=26077&mode=view
... I don't extract icons... These are from FileView.

The smudged look is most probably due to I use H and W 42 and not 48.
from the Realm of Death
User avatar
sl23
Posts: 1832
Joined: February 17th, 2011, 7:45 pm
Location: a Galaxy S7 far far away

Re: Plugin DriveInfo

Post by sl23 »

death.crafter wrote: October 25th, 2021, 10:46 am Because, it has inline functions. You don't have to create 10 measures to get 10 drive infos. One is enough :Whistle

Also, I don't have this handicap,
Screenshot 2021-10-25 162305.png
and the name is more legible.
Ah ok. Thanks for sharing, I'll give it a try at some point :thumbup:
57686174 77696C6C 6265 77696C6C 6265
User avatar
sl23
Posts: 1832
Joined: February 17th, 2011, 7:45 pm
Location: a Galaxy S7 far far away

Re: Plugin DriveInfo

Post by sl23 »

Nicely done! Credit to death.crafter, nice skin and to CodeCode for the lua code :thumbup:

I added this to make "Back" easier:
X1MouseUpAction=[!CommandMeasure FileView "PreviousFolder"][!UpdateMeasure *][!UpdateMeter *][!Update]
57686174 77696C6C 6265 77696C6C 6265
User avatar
deflore08
Posts: 209
Joined: July 12th, 2020, 7:47 am
Location: Ada, Garden City

Re: Plugin DriveInfo

Post by deflore08 »

Woooooooooww!! GREAT job! Thanks! :o
Image * Image * Image * Image
User avatar
death.crafter
Rainmeter Sage
Posts: 1398
Joined: April 24th, 2021, 8:13 pm

Re: Plugin DriveInfo

Post by death.crafter »

deflore08 wrote: October 29th, 2021, 5:04 pm Woooooooooww!! GREAT job! Thanks! :o
Thanks... 😄
from the Realm of Death