获取硬盘序列号

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

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; } 

资源

希望这可以帮助 :)

我在项目中使用了以下方法,并且成功运行。

 private string identifier(string wmiClass, string wmiProperty) //Return a hardware identifier { string result = ""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } return result; } 

你可以调用上面提到的方法,

 string modelNo = identifier("Win32_DiskDrive", "Model"); string manufatureID = identifier("Win32_DiskDrive", "Manufacturer"); string signature = identifier("Win32_DiskDrive", "Signature"); string totalHeads = identifier("Win32_DiskDrive", "TotalHeads"); 

如果您需要唯一的标识符,请使用这些ID的组合。

@ Sprunth的答案有一个简单的方法。

 private void GetAllDiskDrives() { var 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.InterfaceType = wmi_HD["InterfaceType"].ToString(); hd.Caption = wmi_HD["Caption"].ToString(); hd.SerialNo =wmi_HD.GetPropertyValue("SerialNumber").ToString();//get the serailNumber of diskdrive hdCollection.Add(hd); } } public class HardDrive { public string Model { get; set; } public string InterfaceType { get; set; } public string Caption { get; set; } public string SerialNo { get; set; } } 

使用“vol”shell命令并从其输出中parsing序列,就像这样。 至less在Win7中工作

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CheckHD { class HDSerial { const string MY_SERIAL = "F845-BB23"; public static bool CheckSerial() { string res = ExecuteCommandSync("vol"); const string search = "Number is"; int startI = res.IndexOf(search, StringComparison.InvariantCultureIgnoreCase); if (startI > 0) { string currentDiskID = res.Substring(startI + search.Length).Trim(); if (currentDiskID.Equals(MY_SERIAL)) return true; } return false; } public static string ExecuteCommandSync(object command) { try { // create the ProcessStartInfo using "cmd" as the program to be run, // and "/c " as the parameters. // Incidentally, /c tells cmd that we want it to execute the command that follows, // and then exit. System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); // The following commands are needed to redirect the standard output. // This means that it will be redirected to the Process.StandardOutput StreamReader. procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; // Do not create the black window. procStartInfo.CreateNoWindow = true; // Now we create a process, assign its ProcessStartInfo and start it System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); // Get the output into a string string result = proc.StandardOutput.ReadToEnd(); // Display the command output. return result; } catch (Exception) { // Log the exception return null; } } } } 

下面是获取硬盘序列号的全function方法:

 public string GetHardDiskSerialNo() { ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection mcol = mangnmt.GetInstances(); string result = ""; foreach (ManagementObject strt in mcol) { result += Convert.ToString(strt["VolumeSerialNumber"]); } return result; } 

我正在使用这个:

 <!-- language: c# --> private static string wmiProperty(string wmiClass, string wmiProperty){ using (var searcher = new ManagementObjectSearcher($"SELECT * FROM {wmiClass}")) { try { IEnumerable<ManagementObject> objects = searcher.Get().Cast<ManagementObject>(); return objects.Select(x => x.GetPropertyValue(wmiProperty)).FirstOrDefault().ToString().Trim(); } catch (NullReferenceException) { return null; } } } 

以下是一些可能有所帮助的代码:

 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); string serial_number=""; foreach (ManagementObject wmi_HD in searcher.Get()) { serial_number = wmi_HD["SerialNumber"].ToString(); } MessageBox.Show(serial_number); 

我发现的最好的方法是,从这里下载一个DLL

然后,将dll添加到您的项目。

然后,添加代码:

 [DllImportAttribute("HardwareIDExtractorC.dll")] public static extern String GetIDESerialNumber(byte DriveNumber); 

然后,从您需要的地方调用硬盘ID

 GetIDESerialNumber(0).Replace(" ", string.Empty); 

注意:进入资源pipe理器中的DLL的属性,并将“构build操作”设置为“embedded式资源”