问题
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