views:

914

answers:

3

Morning all,

How would I go about getting the serial number of the storage/sd card on my mobile device? I am using C#.Net 2.0 on the Compact Framework V2.0.

Thanks.

+1  A: 

I'm not on a machine that I can get the syntax for you but I believe you can use the System.IO namespace to get IO based attributes.

First get a DirectoryInfo to the storage card. (I'm not 100% on the code here, you may need to test, I'll update it if I can get to my machine)

public DirectoryInfo GetStorageCard() {
    DirectoryInfo deviceRoot = new DirectoryInfo("/");
    foreach (DirectoryInfo dir in deviceRoot.GetDirectories()) 
       if (dir.Attributes == FileAttributes.Directory & dir.Attributes = FileAttributes.Temporary)
return dir;
}

Then check all the properties on the DirectoryInfo that code returns. Thanks to the joys of debugging you should be able to see if the serial number is one of the available properties.

If not, you may need to look to something a bit more unmanaged.

Hope this helps.

TreeUK
+1  A: 

You need to use some unmanaged APIs. Specifically, DeviceIoControl using a 'control code' of IOCT_DISK_GET_STORAGEID. This will, in turn, return a STORAGE_IDENTIFICATION structure.

Here's where it gets a bit tricky, as STORAGE_IDENTIFICATION uses a property (dwSerialNumOffset) to specify the (memory) offset from the beginning of the structure, which would be difficult to translate into interop calls.

Edit: Found a VB.NET implementation on the MSDN forums

Richard Szalay
The last post by joOls in that implementation thread says that depending on hardware it might only return the windows temporary generated serial, not the hardware serial (just to be aware of).
TreeUK
@TreeUK: Well spotted mate, thanks for the heads up.
Richard Szalay
+2  A: 

The Smart Device Framework provides a mechanism to get SD Card serial numbers and manufacturer IDs.

ctacke