【发布时间】:2010-10-01 18:57:34
【问题描述】:
如何在 C# 中获取 USB-Stick 或 USB-HardDrive 的内部序列号?
【问题讨论】:
如何在 C# 中获取 USB-Stick 或 USB-HardDrive 的内部序列号?
【问题讨论】:
试试这个:
// add a reference to the System.Management assembly and
// import the System.Management namespace at the top in your "using" statement.
// Then in a method, or on a button click:
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject currentObject in theSearcher.Get())
{
ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());
}
【讨论】:
使用Win32的解决方案is described here
编辑:原始链接似乎已丢失。以上为缓存副本,作者还在VB.Net中写了一些示例代码still online here。
【讨论】:
我对 Yuval Adam 提供的解决方案有疑问,因为我尝试的每个 USB 记忆棒在 Windows 7 上都返回空白。
我通过查看当前对象的属性 PNPDeviceId 解决了这个问题。
例如
currentObject["PNPDeviceID"].ToString();
不确定这是否有效,但它在我尝试过的 3 个 USB 记忆棒上对我有用
【讨论】: