添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using WMI to get info about hard drives on a computer but I just can't find the property that will allow me to identify which hard drive is used as system drive where Windows is installed.

ManagementObjectSearcher mos_HDD = new ManagementObjectSearcher("select * from Win32_DiskDrive");

I tried iterating through all properties but neither one looks like it holds the info I need.

foreach (ManagementObject mo_HDD in mos_HDD.Get())
      Console.WriteLine("HDD Properties:");
      foreach (PropertyData pd in mo_HDD.Properties)
           Console.WriteLine("\tName: {0} \tValue: {1}", pd.Name, pd.Value != null ? pd.Value.ToString() : "NULL");

I've also looked at MSDN documentation but w/o luck.

What I'm trying to do here is to get some kind of identifier for a system drive (such as Signature or Serial number).

Any help to get this info is highly appreciated.

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive"); foreach (ManagementObject queryObj in searcher.Get()) Console.WriteLine("SerialNumber: {0}", queryObj["SerialNumber"]); Console.WriteLine("Signature: {0}", queryObj["Signature"]); catch (ManagementException e)

Your management object will have those properties in it.

If you're getting null for those values then the problem is that you're using a version of Windows that doesn't support them.

SerialNumber
Windows Server 2003 and Windows XP:  This property is not available.

In which case you have to use the Signature property, but this requires XP SP3 I believe.

Thanks for the details. I checked both of these and SerialNumber looks just fine but the problem is when there are several hard disks in the system. How will I know which one holds the system partition? – Dragan Radivojevic Aug 1, 2013 at 21:22 Find the drive containing a Win32_LogicalDisk where the DeviceID == C: (or whatever drive letter Windows is installed on) – PhonicUK Aug 2, 2013 at 12:49 I couldnt find the solution to this but this is the closest as it gets so I'm marking it as answer. – Dragan Radivojevic Sep 23, 2013 at 18:30

There is a class Win32_LogicalDisk similar to Win32_Diskdrive which has the volumes, rather than the physical disks. It has a name property which has the drive letter.

The environment variable %SystemRoot% has the path where windows is (e.g. D:\windows) Also in registry Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion SystemRoot.

So you can compare SystemRoot drive letter to the Name property from Win32_LogicalDisk.

I believe that there is a way to get a list of volumes from the properties of Win32_Diskdrive but I've not tried it.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.