获取硬盘序列号 [英] Get Hard disk serial Number
本文介绍了获取硬盘序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要得到硬盘序列号。我如何我能做到这一点?
我试图用两个code,但我没有得到
I want to get hard disk serial number. How I can I do that? I tried with two code but I am not getting
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>");
}
}
在这其中,我没有得到任何唯一的序列号。结果
而第二个是
In this one I am not getting any Unique Serial number.
And Second one is
string drive = "C";
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive + ":\"");
disk.Get();
Label3.Text = "VolumeSerialNumber="+ disk["VolumeSerialNumber"].ToString();
在这里,我得到 VolumeSerialNumber
。但它不是唯一的。如果我格式化硬盘,这将改变。我怎样才能得到呢?
Here I am getting VolumeSerialNumber
. But it is not unique one. If I format the hard disk, this will change. How Can I get this?
推荐答案
嗯,看你的第一套code,我想你已经检索(也许?)硬盘的型号。该序列号来自 Win32_PhysicalMedia
。
Hm, looking at your first set of code, I think you have retrieved (maybe?) the hard drive model. The serial # comes from 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;
}
希望这有助于:)
这篇关于获取硬盘序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文