Josh Dycus

Josh Dycus

  • NA
  • 11
  • 20.5k

Hard Drive Information with WMI

Oct 18 2010 5:01 PM
Hello,

I am trying to retrieve system information using WMI.  Everything is working good until I got to the Hard Drive. I am trying to populate the Drive size for the local Hard Drives on the system.  The issue is at runtime it only populate the first drive and does not go beyond that.  I am still fairly new to C#.  Here is the snippet for the Hard Drive:

public string GetHardDrive()
        {
          

            double disk = 0;
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_LogicalDisk where DriveType=3");
            foreach (ManagementObject wmi in searcher.Get())
            {
                try
                {
                     disk = Math.Round(((((double)Convert.ToDouble(wmi["Size"]) / 1024) / 1024) / 1024), 2);
                     return disk.ToString();
                }
                catch
                {
                }
            }
            return "Unable to Find Drive";
        }

Any Help would be appreciated.

Thanks in advance.

Answers (7)