19

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

4

3 回答 3

33

试试这个:

// 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/

于 2009-01-16T10:32:11.213 回答
9

此处描述了使用 Win32 的解决方案

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

于 2009-01-16T10:59:35.463 回答
7

我对 Yuval Adam 提供的解决方案有疑问,因为我尝试的每个 USB 记忆棒在 Windows 7 上都返回空白。

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

例如

currentObject["PNPDeviceID"].ToString();

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

于 2011-05-28T20:06:26.563 回答