c#WINFORM获取硬盘物理序列号 不要逻辑序列号,要物理序列号,希望可以给出完整代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 取第一块硬盘编号 /// </summary> /// <returns></returns> public static string GetHardDiskID() { try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); string strHardDiskID = null; foreach (ManagementObject mo in searcher.Get()) { strHardDiskID = mo["SerialNumber"].ToString().Trim(); break; } return strHardDiskID; } catch { return ""; } } http://hi.baidu.com/libinguest/blog/item/a2574508a46a56960a7b82a2.html http://hi.baidu.com/libinguest/blog/item/a2574508a46a56960a7b82a2.html 最近作软件注册,收集了很多.NET相关的获取硬盘物理序列号的方法,主要分为使用WMI方式和API方式。但这些方法均可能有问题。1,使用WMI方式,有的机器根本取不到硬盘序列号,有的方式在Vista下面会报错。常用的使用WMI的方式主要有下面一些方式:class HardDrive { private string model = null; private string type = null; private string serialNo = null; public string Model { get {return model;} set {model = value;} } public string Type { get {return type;} set {type = value;} } public string SerialNo { get {return serialNo;} set {serialNo = value;} } } class TestProgram { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { //在Vista下面失败 ArrayList hdCollection = new ArrayList(); 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; } // Display available hard drives foreach(HardDrive hd in hdCollection) { Console.WriteLine("Model\t\t: " + hd.Model); Console.WriteLine("Type\t\t: " + hd.Type); Console.WriteLine("Serial No.\t: " + hd.SerialNo); Console.WriteLine(); } // Pause application Console.WriteLine("Press [Enter] to exit..."); Console.ReadLine(); } } 上面的方式先查询Win32_DiskDrive,然后再查询 Win32_PhysicalMedia,经过测试,这种方式不能保证在所有机器上均取得硬盘序列号,而且在Vista下面还会出错,程序直接抛出无法处理的异常。 另外,还可以使用另外一WMI方式,就是查询 PNPDeviceID 的 signature,代码如下:/// <summary> /// 获取硬盘唯一序列号(不是卷标号),可能需要以管理员身份运行程序 /// </summary> /// <returns></returns> public static string GetHdId() { ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher(); /* * PNPDeviceID 的数据是由四部分组成的: 1、接口,通常有 IDE,ATA,SCSI; 2、型号 3、(可能)驱动版本号 4、(可能)硬盘的出厂序列号 * * */ //signature 需要程序以管理员身份运行(经过测试,2003系统上非管理员身份也可以运行,查相关资料说,可能在2000系统上获取的值为空) wmiSearcher.Query = new SelectQuery( "Win32_DiskDrive", "", new string[] { "PNPDeviceID", "signature" } ); ManagementObjectCollection myCollection = wmiSearcher.Get(); ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator(); em.MoveNext(); ManagementBaseObject mo = em.Current; //string id = mo.Properties["PNPDeviceID"].Value.ToString().Trim(); string id = mo.Properties["signature"].Value.ToString().Trim(); return id; } 有人说,使用 signature 需要程序以管理员身份运行(经过测试,2003系统上非管理员身份也可以运行),而且查询相关资料说,可能在2000系统上获取的值为空。使用这种方式,在Vista上面工作良好。经过测试,使用 signature 均能够取得硬盘序列号,但是跟 Win32_PhysicalMedia 查询出来的号不一样。目前我也不能肯定 使用 signature 能够100%取道硬盘序列号。 使用WMI方式需要客户机开启WMI服务,但这个往往不能保证,所以使用这种方式有一定局限性。 更多看这里:IMBA 参考这个:原帖地址:http://topic.csdn.net/u/20100514/17/8c845397-7fcf-4c0f-aaf9-0b1918047a58.html?seed=1234331917&r=65439901#r_65439901代码下载:http://dodosoftware.net/Laboratory/HardDiskSN/GetHDSN.zip 用了"vssvss"和"computerfox"两位朋友的相关源码,测试得到的结果都不一样,都不知哪个才是正确的了!----------三易通软件(服装进销存,服装进销存软件,服装进销存管理软件,服装进销存管理系统,服装店管理软件,服装店管理系统,服装销售管理软件,服装销售管理系统,服装零售管理软件,服装零售管理系统,服装店软件,服装店收银软件):http://www.3etsoft.cn c#鼠标穿透控件如何实现 多线程显示右键菜单的问题 C# 如何获得系统默认的文件关联方式 正则问题第五问 求助c#IO处理ini配置文件 多个where条件语句怎么写? 用linq增加数据时"服务器无法生成类型为“TinyInt”的主键列。" 1000元求购打印货物运单 求一个ADO.net连接数据库的实例 如何将多个Dll打包成一个dll? 在WEB窗体下有没有像WINDOWS窗体下跟DateTimePicker一样的日期/时间控件啊? 获取系统唯一值。。。。
/// 取第一块硬盘编号
/// </summary>
/// <returns></returns>
public static string GetHardDiskID()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
string strHardDiskID = null;
foreach (ManagementObject mo in searcher.Get())
{
strHardDiskID = mo["SerialNumber"].ToString().Trim();
break;
}
return strHardDiskID;
}
catch
{
return "";
}
}
最近作软件注册,收集了很多.NET相关的获取硬盘物理序列号的方法,主要分为使用WMI方式和API方式。但这些方法均可能有问题。1,使用WMI方式,有的机器根本取不到硬盘序列号,有的方式在Vista下面会报错。常用的使用WMI的方式主要有下面一些方式:class HardDrive
{
private string model = null;
private string type = null;
private string serialNo = null; public string Model
{
get {return model;}
set {model = value;}
} public string Type
{
get {return type;}
set {type = value;}
} public string SerialNo
{
get {return serialNo;}
set {serialNo = value;}
}
} class TestProgram
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//在Vista下面失败
ArrayList hdCollection = new ArrayList(); 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;
} // Display available hard drives
foreach(HardDrive hd in hdCollection)
{
Console.WriteLine("Model\t\t: " + hd.Model);
Console.WriteLine("Type\t\t: " + hd.Type);
Console.WriteLine("Serial No.\t: " + hd.SerialNo);
Console.WriteLine();
} // Pause application
Console.WriteLine("Press [Enter] to exit...");
Console.ReadLine();
}
}
上面的方式先查询Win32_DiskDrive,然后再查询 Win32_PhysicalMedia,经过测试,这种方式不能保证在所有机器上均取得硬盘序列号,而且在Vista下面还会出错,程序直接抛出无法处理的异常。 另外,还可以使用另外一WMI方式,就是查询 PNPDeviceID 的 signature,代码如下:/// <summary>
/// 获取硬盘唯一序列号(不是卷标号),可能需要以管理员身份运行程序
/// </summary>
/// <returns></returns>
public static string GetHdId()
{
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher();
/*
* PNPDeviceID 的数据是由四部分组成的:
1、接口,通常有 IDE,ATA,SCSI;
2、型号
3、(可能)驱动版本号
4、(可能)硬盘的出厂序列号
*
*
*/
//signature 需要程序以管理员身份运行(经过测试,2003系统上非管理员身份也可以运行,查相关资料说,可能在2000系统上获取的值为空) wmiSearcher.Query = new SelectQuery(
"Win32_DiskDrive",
"",
new string[] { "PNPDeviceID", "signature" }
);
ManagementObjectCollection myCollection = wmiSearcher.Get();
ManagementObjectCollection.ManagementObjectEnumerator em =
myCollection.GetEnumerator();
em.MoveNext();
ManagementBaseObject mo = em.Current;
//string id = mo.Properties["PNPDeviceID"].Value.ToString().Trim();
string id = mo.Properties["signature"].Value.ToString().Trim();
return id;
}
有人说,使用 signature 需要程序以管理员身份运行(经过测试,2003系统上非管理员身份也可以运行),而且查询相关资料说,可能在2000系统上获取的值为空。使用这种方式,在Vista上面工作良好。经过测试,使用 signature 均能够取得硬盘序列号,但是跟 Win32_PhysicalMedia 查询出来的号不一样。目前我也不能肯定 使用 signature 能够100%取道硬盘序列号。 使用WMI方式需要客户机开启WMI服务,但这个往往不能保证,所以使用这种方式有一定局限性。
原帖地址:http://topic.csdn.net/u/20100514/17/8c845397-7fcf-4c0f-aaf9-0b1918047a58.html?seed=1234331917&r=65439901#r_65439901代码下载:
http://dodosoftware.net/Laboratory/HardDiskSN/GetHDSN.zip
三易通软件(服装进销存,服装进销存软件,服装进销存管理软件,服装进销存管理系统,服装店管理软件,服装店管理系统,服装销售管理软件,服装销售管理系统,服装零售管理软件,服装零售管理系统,服装店软件,服装店收银软件):http://www.3etsoft.cn