This is something I meant to post up ages ago as information and solutions are hard to come by on this specific topic.
The problem all stems from the fact that the USB specification does not require a hard coded serial number as mandatory for USB devices, especially memory sticks although in my tests 99% of sticks did in fact have an OEM serial number.
Windows secifies its own serial number for each device which is volatile meaning the serial number is erased and renewed everytime the USB stick is formatted. This causes problems when your using your stick as a security device or dongle. Basically, in Windows if you get 2 devices from the same manufacturer without OEM numbers, it gets confused. Not sure about a Mac or Linux.
Anyways, to the point of this article.....how do we actually get the OEM serail so a device can be formatted over and over again and you can still tell the drive apart from another one?
Well, there are 2 ways. Using the registry or using something called a Win32 WMI Provider. WMI is a database of all aspects of your operating system and hardware and is used by Windows to hold everything together. Infact the WMI is a all held in the regisrty but the provider using management classes lets you query it using SQL.
You need to add a reference to your C Sharp program to SYSTEM.MANAGEMENT.
Step 1 is to query the Win32_LogicalDiskToPartition section and get all the logical disk names e.g: F:\ and mapped enumeration which have a big long string attached like so : 000000001
This will give us the enumeration of the drive so we can then strip off the int at the end and query the Win32_DiskDrive. The drive we are looking for will be in the form \\\\.\\PHYSICALDRIVE(enumeration(enumeration) where (enumeration) is the bit we found out above. The PNPDeviceID is the full non-volatile serial number which with a bit of parsing we can retrieve the actual serial number.
It may sound confusing reading the above so the best thing to do is look at the attached source. I have wrapped it all up into a class that can be called as follows:
USBSerialNumber usb = new USBSerialNumber();
string serial = usb.getSerialNumberFromDriveLetter("f:\"
You can download the class by clicking the image below:
Hope you like it and it saves you some time!