Question

I need to return the DeviceID of the System Reserved partition. I should be able to do this with the Win32_Volume class by first getting the Label property, and if it matches "System Reserved" then get the DeviceID property. The following code crashes with a null reference exception:

static void Main(string[] args)
{
    ManagementObjectSearcher ms = new ManagementObjectSearcher("Select * From Win32_Volume");
    foreach (ManagementObject mo in ms.Get())
    {
        if (mo["Label"].ToString() == "System Reserved")
        {
            Console.WriteLine(mo["DeviceID"].ToString());
        }
    }
    Console.Read();
}
Was it helpful?

Solution

Here it is for anyone that needs to do this:

string sysGuid = "";
        try
        {
            ManagementObjectSearcher ms = new ManagementObjectSearcher("SELECT * FROM Win32_Volume");
            foreach (ManagementObject mo in ms.Get())
            {
                if (mo["Label"].ToString() == "System Reserved")
                {
                    sysGuid = mo["DeviceID"].ToString();
                    break;
                }
            }
        }
        catch (Exception) {}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top