问题描述
我已经兜兜转转了一会儿,似乎也无法在谷歌上找到答案.
I've been going around in circles for a bit now, can't seem to find the answer on google either.
正如标题所说,如果我得到当前正在运行的驱动器号窗口,让我们这样说:Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System));
然后我可以确定它的 Win32_DiskDrive SerialNumber 吗?我找不到链接它们的方法.
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.
那是制造商的 S/N,而不是 VolumeSerialNumber.
That is the manufacturer's S/N not the VolumeSerialNumber.
提前致谢
推荐答案
您可以使用 ManagmentObjectSearch 结合 协会成员声明:
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;
}
用法:
var num = GetSerialNumber(Path.GetPathRoot(Environment.SystemDirectory).TrimEnd(new [] {'\\'}));
注意:不要忘记从 Path.GetPathRoot
返回的路径中删除反斜杠.
Note: Dont forget to remove backslashes from the path returned by Path.GetPathRoot
.
这篇关于是否可以确定 Environment.SpecialFolder.System 驱动器的 Win32_DiskDrive SerialNumber?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!