如何在C#中获取USB-Stick的序列号

时间:2009-01-16 10:26:40

标签: c# usb usb-drive

如何在C#中获取USB-Stick或USB-HardDrive的内部序列号?

3 个答案:

答案 0 :(得分:28)

试试这个:

// 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());
}

来源:http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/f4447ed3-7e5f-4635-a28a-afff0b620156/

答案 1 :(得分:9)

使用Win32 is described here

的解决方案

编辑:原始链接似乎已经丢失。以上是一个缓存副本,作者还在VB.Net中编写了一些示例代码still online here

答案 2 :(得分:6)

我遇到了Yuval Adam提供的解决方案的问题,因为我试过的每个USB记忆棒在Windows 7上都是空白的。

我通过查看当前对象的属性PNPDeviceId来解决这个问题。

E.g。

currentObject["PNPDeviceID"].ToString();

不确定这是多么有效,但它在我试过的3个USB棒上对我有用

相关问题