First, I will say that if you buy SDF ($ 50), you get a source for this function, which you could then pull into your project and confuse.
If, however, you have more time than money, and you want to realize it yourself, I can point you in the right direction. I leave it for you to align all these parts together, but here are the steps:
First you must open the volume as follows:
IntPtr hFile = NativeMethods.CreateFile( string.Format("{0}\\Vol:", driveName), NativeMethods.GENERIC_READ, 0, 0, NativeMethods.OPEN_EXISTING, 0, 0);
This gives you the ability to manage the drive. Then you just call IOCTL to get the repository id:
byte[] data = new byte[512]; int returned; int result = NativeMethods.DeviceIoControl( hFile, IOCTL_DISK_GET_STORAGEID, IntPtr.Zero, 0, data, data.Length, out returned, IntPtr.Zero); if (result != 0) { m_storageID = STORAGE_IDENTIFICATION.FromBytes(data); }
This gives you a binary representation of the STORAGE_IDENTIFICATION structure. Note that the structure is really just a description of the data header. It contains an offset to the actual serial number data, which is an ASCII string. The extraction looks something like this:
int snoffset = BitConverter.ToInt32(data, 12); string sn = Encoding.ASCII.GetString(data, snoffset, data.Length - snoffset); return sn.TrimEnd(new char[] { '\0' });