1

我需要返回系统保留分区的设备 ID。我应该能够通过首先获取 Label 属性来使用 Win32_Volume 类执行此操作,如果它与“系统保留”匹配,则获取 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();
}
4

1 回答 1

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) {}
于 2012-10-02T12:47:47.127 回答