需要获取System Reserved分区的名称(DeviceID)

时间:2012-10-01 17:56:43

标签: c# wmi drive disk-partitioning

我需要返回System Reserved分区的DeviceID。我应该能够通过首先获取Label属性来使用Win32_Volume类,如果它匹配“System Reserved”,则获取DeviceID属性。以下代码与空引用异常崩溃:

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();
}

1 个答案:

答案 0 :(得分:2)

这是任何需要这样做的人:

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) {}