简体   繁体   中英

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

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

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/

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 .

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.

Eg

currentObject["PNPDeviceID"].ToString();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM