如何在C#中获取USB-Stick的序列号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/450009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get serial number of USB-Stick in C#
提问by Xn0vv3r
How do I get the internal serial number of a USB-Stick or USB-HardDrive in C#?
如何在 C# 中获取 USB-Stick 或 USB-HardDrive 的内部序列号?
采纳答案by Yuval Adam
Try this:
尝试这个:
// 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/
回答by James L
A solution using Win32 is described here
此处描述了使用 Win32 的解决方案
Edit: the original link seems to have gone missing. The above is a cached copy, and the author also wrote some sample code in VB.Net which is still online here.
编辑:原始链接似乎丢失了。以上是缓存副本,作者也在VB.Net中写了一些示例代码,这里还在网上。
回答by James
I had problems with the solution offered by Yuval Adam as every USB stick I tried return blank on windows 7.
我遇到了 Yuval Adam 提供的解决方案的问题,因为我尝试过的每个 USB 记忆棒在 Windows 7 上都返回空白。
I solved this by just looking at the property PNPDeviceId on the current object.
我通过查看当前对象上的属性 PNPDeviceId 解决了这个问题。
E.g.
例如
currentObject["PNPDeviceID"].ToString();
Not sure how valid this is but it worked for me on the 3 USB Sticks I tried
不确定这有多有效,但它在我尝试过的 3 个 USB 记忆棒上对我有用