如何使用 C# 获取硬盘序列号?
Posted
技术标签:
【中文标题】如何使用 C# 获取硬盘序列号?【英文标题】:How do I use C# to get the Hard-disk serial number? 【发布时间】:2010-11-24 03:26:41 【问题描述】:如何在不使用dll且VISTA支持的情况下获取硬盘序列号
【问题讨论】:
【参考方案1】:using System.Management;
public string GetHDDSerial()
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject wmi_HD in searcher.Get())
// get the hardware serial no.
if (wmi_HD["SerialNumber"] != null)
return wmi_HD["SerialNumber"].ToString();
return string.Empty;
【讨论】:
【参考方案2】:这是适合我的代码:
ManagementObjectSearcher searcher = null;
searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
HardDrive hd = new HardDrive();
try
txtmdl.Text = hd.Caption = wmi_HD["Caption"].ToString();
txtsn.Text=(hd.SerialNo = wmi_HD.GetPropertyValue("SerialNumber").ToString());
我刚刚添加了 2 个文本框来获得结果,你也可以这样做!
和硬盘类:
public class HardDrive
public string Model get; set;
public string InterfaceType get; set;
public string Caption get; set;
public string SerialNo get; set;
别忘了使用:
using System.Management;
来源: get serial number of hard disk in c#
【讨论】:
【参考方案3】:试试here 的这段代码,如果它有效,请告诉我们:
// Namespace Reference
using System.Management;
/// <summary>
/// method to retrieve the selected HDD's serial number
/// </summary>
/// <param name="strDriveLetter">Drive letter to retrieve serial number for</param>
/// <returns>the HDD's serial number</returns>
public string GetHDDSerialNumber(string drive)
//check to see if the user provided a drive letter
//if not default it to "C"
if (drive == "" || drive == null)
drive = "C";
//create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive +":\"");
//bind our management object
disk.Get();
//return the serial number
return disk["VolumeSerialNumber"].ToString();
编辑:如果这不起作用,请尝试 CodeProject 中的这段代码:
首先,让我们创建一个类来存储有关硬盘的信息:
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;
接下来,我们查询 Win32_DiskDrive 类:
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);
现在我们需要从 Win32_PhysicalMedia 中提取序列号 类:
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();
【讨论】:
【参考方案4】:您好,刚刚找到此链接
它对我有用:
enter link description here
这是代码的基本部分:
/// <summary>
/// return Volume Serial Number from hard drive
/// </summary>
/// <param name="strDriveLetter">[optional] Drive letter</param>
/// <returns>[string] VolumeSerialNumber</returns>
public string GetVolumeSerial(string strDriveLetter)
if( strDriveLetter=="" || strDriveLetter==null)
strDriveLetter="C";
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter +":\"");
disk.Get();
return disk["VolumeSerialNumber"].ToString();
【讨论】:
以上是关于如何使用 C# 获取硬盘序列号?的主要内容,如果未能解决你的问题,请参考以下文章