How to get serial number of USB-Stick in C#

橙三吉。 提交于 2019-11-26 15:26:54

问题


How do I get the internal serial number of a USB-Stick or USB-HardDrive in C#?


回答1:


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

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




回答2:


A solution using Win32 is described here

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.




回答3:


I had problems with the solution offered by Yuval Adam as every USB stick I tried return blank on windows 7.

I solved this by just looking at the property PNPDeviceId on the current object.

E.g.

currentObject["PNPDeviceID"].ToString();

Not sure how valid this is but it worked for me on the 3 USB Sticks I tried



来源:https://stackoverflow.com/questions/450009/how-to-get-serial-number-of-usb-stick-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!