Is it possible to determine the Win32_DiskDrive SerialNumber of the Environment.SpecialFolder.System drive?

一笑奈何 提交于 2020-01-02 17:36:01

问题


I've been going around in circles for a bit now, can't seem to find the answer on google either.

As the title says, if i get the current drive letter windows is running on, let's say like this: Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System)); Can i then determine its Win32_DiskDrive SerialNumber? I cannot find a way to link them.

That is the manufacturer's S/N not the VolumeSerialNumber.

Thanks in advance


回答1:


You can use ManagmentObjectSearch combined with ASSOCIATORS OF statement:

public static string GetSerialNumber(string logicalDrive)
{
    using (var partitionsQuery = new ManagementObjectSearcher(string.Format("ASSOCIATORS OF {{Win32_LogicalDisk.DeviceID='{0}'}} WHERE ResultClass = Win32_DiskPartition", logicalDrive)))            
    {
        foreach (var results in partitionsQuery.Get())
        {
            using (var diskDrives = new ManagementObjectSearcher(string.Format("ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{0}'}} WHERE ResultClass=Win32_DiskDrive", results["DeviceID"])))
            {
                foreach (var d in diskDrives.Get())
                {
                    Console.WriteLine("Serial: " + d["SerialNumber"]);

                    return d["SerialNumber"].ToString();
                }
            }
        }
    }

    return null;
} 

Usage:

var num = GetSerialNumber(Path.GetPathRoot(Environment.SystemDirectory).TrimEnd(new [] {'\\'}));

Note: Dont forget to remove backslashes from the path returned by Path.GetPathRoot.




回答2:


You could use the ManagementObjectSearcher class from System.Management then loop through the properties to find the serial number.

I think something along these lines will get you close to what you're looking for...

                var search = new ManagementObjectSearcher("select * from Win32_DiskDrive");
                foreach (var mo in search.Get())
                {
                    if (mo["SerialNumber"] != null)
                    {
                        return mo["SerialNumber"].ToString();
                    }
                }

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-diskdrive

^ that should contain all the various properties you can get

Hope that helps.



来源:https://stackoverflow.com/questions/57558122/is-it-possible-to-determine-the-win32-diskdrive-serialnumber-of-the-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!