Retrieving Non-Volatile USB Serial Number Using C# .NET
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:\"
;
MessageBox.Show(serial);
You can download the class by clicking the image below:
Hope you like it and it saves you some time!
UPDATE Thursday 10th May 2007
You can download a test project from here : USBSerialNumberTest.zip

Comments
it working but can you give me some idea that in vc how can we get serial number for removal drive
The type or namespace 'USBSerialNumber' could not be found(are you missing a directive or an assembly reference?)
I'm using VS 2005.
USBSerialNumber.USBSerialNumber usb = new USBSerialNumber.USBSerialNumber();
string serial = usb.getSerialNumberFromDriveLetter("f:";
MessageBox.Show(serial);
You could rename the namespace in the class to the same as your project also.
Dan.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
USBDriveSerialNumber.USBSerialNumber usb = new
USBDriveSerialNumber.USBSerialNumber();
string serial = usb.getSerialNumberFromDriveLetter("F:\");
this.textBox1.Text = serial;
}
Thank to Daniel Mackey to show the power of the unfortunaly unknowned WMI.
http://www.msdner.com/dev-archive/160/12-44-1601927.shtm
"A support request has confirmed through the WMI dev team: Win32_DiskPartition query requires elevated rights. No Exception is raised, just returns as if no Partition exists."
is there any way to Deny or Grant the hard reset for a user on a PDA? (I mean like switching it On/OFF)
if no way to do the above, can I store an application in a Non-Volatile memory, and AutoStart the application even after a Hard reset?
i'd like to ask is there a way to get-rid of searching from the floppy. becos everytime when searching the floppy drive bulb flashing on. is there a way to stop that....
hope a reply from you.
Thanks in Advance........ :-)
Thanks for shairing the knowledge. thats is a act of a god.
or a c# dll that can be used in vb.60
Thank u Soooooooooooooooooooo Muchhhhhh for your code.
It Really helped me .
Warm Regards
Vibhu
http://www.msdner.com/dev-archive/160/12-44-1601927.shtm
"A support request has confirmed through the WMI dev team: Win32_DiskPartition query requires elevated rights. No Exception is raised, just returns as if no Partition exists."
msdner.com is move to www.itags.org