Compact Framework - Get Storage Card Serial Number

2019-02-15 08:18发布

问题:

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:

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



回答2:

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.



回答3:

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