gpt4 book ai didi

c# - 获取硬盘序列号

转载 作者:IT王子 更新时间:2023-10-29 04:15:52 26 4
gpt4 key购买 nike

我想获取硬盘序列号。我该怎么做?我尝试了两个代码,但我没有得到

StringCollection propNames = new StringCollection();
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
PropertyDataCollection props = driveClass.Properties;
foreach (PropertyData driveProperty in props)
{
propNames.Add(driveProperty.Name);
}
int idx = 0;
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drv in drives)
{
Label2.Text+=(idx + 1);
foreach (string strProp in propNames)
{
//Label2.Text+=drv[strProp];
Response.Write(strProp + " = " + drv[strProp] + "</br>");
}
}

在这个中我没有得到任何唯一序列号。
第二个是

string drive = "C";
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive + ":\"");
disk.Get();
Label3.Text = "VolumeSerialNumber="+ disk["VolumeSerialNumber"].ToString();

我在这里得到 VolumeSerialNumber。但它不是独一无二的。如果我格式化硬盘,这将会改变。我怎样才能得到这个?

最佳答案

嗯,看看你的第一组代码,我想你已经检索到(也许?)硬盘驱动器模型。序列号来自 Win32_PhysicalMedia

获取硬盘型号

    ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
hdCollection.Add(hd);
}

获取序列号

 searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
// get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection[i];

// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
hd.SerialNo = "None";
else
hd.SerialNo = wmi_HD["SerialNumber"].ToString();

++i;
}

Source

希望这有帮助:)

关于c# - 获取硬盘序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4084402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com