WINCE 獲取第一個SD卡

API

[DllImport("coredll", EntryPoint = "FindFirstStore")] public extern static IntPtr FindFirstStore(ref STOREINFO store); [DllImport("coredll", EntryPoint = "FindNextStore")] public extern static bool FindNextStore(IntPtr handle, ref STOREINFO store); [DllImport("coredll", EntryPoint = "FindCloseStore")] public extern static bool FindCloseStore(IntPtr handle); [DllImport("coredll", EntryPoint = "OpenStore")] public extern static IntPtr OpenStore(string deviceName); [DllImport("coredll", EntryPoint = "FindFirstPartition")] public extern static IntPtr FindFirstPartition(IntPtr store, ref PARTINFO part); [DllImport("coredll", EntryPoint = "FindNextPartition")] public extern static bool FindNextPartition(IntPtr part, ref PARTINFO partion); [DllImport("coredll", EntryPoint = "FindClosePartition")] public extern static bool FindClosePartition(IntPtr handle);

static readonly IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
常量

#region 常量定義 數據來源D:\WINCE600\PUBLIC\COMMON\SDK\INC\storemgr.h const int STORE_ATTRIBUTE_READONLY = 0x00000001; const int STORE_ATTRIBUTE_REMOVABLE = 0x00000002; const int STORE_ATTRIBUTE_UNFORMATTED = 0x00000004; const int STORE_ATTRIBUTE_AUTOFORMAT = 0x00000008; const int STORE_ATTRIBUTE_AUTOPART = 0x00000010; const int STORE_ATTRIBUTE_AUTOMOUNT = 0x00000020; const int PARTITION_ATTRIBUTE_EXPENDABLE = 0x00000001; // partition may be trashed const int PARTITION_ATTRIBUTE_READONLY = 0x00000002; // partition is read-only const int PARTITION_ATTRIBUTE_AUTOFORMAT = 0x00000004; const int PARTITION_ATTRIBUTE_ACTIVE = 0x00000008; const int PARTITION_ATTRIBUTE_BOOT = 0x00000008; // Active(DOS) == Boot(CE) const int PARTITION_ATTRIBUTE_MOUNTED = 0x00000010; const int DEVICENAMESIZE = 8; const int STORENAMESIZE = 32; const int FILESYSNAMESIZE = 32; const int FORMATNAMESIZE = 32; const int PARTITIONNAMESIZE = 32; const int PROFILENAMESIZE = 32; const int FOLDERNAMESIZE = 32; const int VOLUMENAMESIZE = 64; const int FSDDESCSIZE = 32; const int STORAGE_DEVICE_TYPE_PCIIDE = (1 << 0); const int STORAGE_DEVICE_TYPE_FLASH = (1 << 1); const int STORAGE_DEVICE_TYPE_ATA = (1 << 2); const int STORAGE_DEVICE_TYPE_ATAPI = (1 << 4); const int STORAGE_DEVICE_TYPE_PCCARD = (1 << 5); const int STORAGE_DEVICE_TYPE_CFCARD = (1 << 6); const int STORAGE_DEVICE_TYPE_SRAM = (1 << 7); const int STORAGE_DEVICE_TYPE_DVD = (1 << 8); const int STORAGE_DEVICE_TYPE_CDROM = (1 << 9); const int STORAGE_DEVICE_TYPE_USB = (1 << 10); const int STORAGE_DEVICE_TYPE_1394 = (1 << 11); const int STORAGE_DEVICE_TYPE_DOC = (1 << 12); const int STORAGE_DEVICE_TYPE_UNKNOWN = (1 << 29); const int STORAGE_DEVICE_TYPE_REMOVABLE_DRIVE = (1 << 30);// Drive itself is removable const int STORAGE_DEVICE_TYPE_REMOVABLE_MEDIA = (1 << 31);// Just the media is removable ex. CDROM, FLOPPY const int STORAGE_DEVICE_FLAG_READWRITE = (1 << 0); const int STORAGE_DEVICE_FLAG_READONLY = (1 << 1); const int STORAGE_DEVICE_FLAG_TRANSACTED = (1 << 2); const int STORAGE_DEVICE_FLAG_MEDIASENSE = (1 << 3); // Device requires media sense calls const int STORAGE_DEVICE_FLAG_XIP = (1 << 4); #endregion

結構體

[StructLayout(LayoutKind.Sequential)] public struct FILETIME { public int dwLowDateTime; public int dwHighDateTime; }; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STOREINFO { public int cbSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DEVICENAMESIZE)] public string szDeviceName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = STORENAMESIZE)] public string szStoreName; public int dwDeviceClass; public int dwDeviceType; public STORAGEDEVICEINFO sdi; public int dwDeviceFlags; public long snNumSectors; public int dwBytesPerSector; public long snFreeSectors; public long snBiggestPartCreatable; public FILETIME ftCreated; public FILETIME ftLastModified; public int dwAttributes; public int dwPartitionCount; public int dwMountCount; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STORAGEDEVICEINFO { public int cbSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = PROFILENAMESIZE)] public string szProfile; public int dwDeviceClass; public int dwDeviceType; public int dwDeviceFlags; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct PARTINFO { public int cbSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = PARTITIONNAMESIZE)] public string szPartitionName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = FILESYSNAMESIZE)] public string szFileSys; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = VOLUMENAMESIZE)] public string szVolumeName; public long snNumSectors; public FILETIME ftCreated; public FILETIME ftLastModified; public int dwAttributes; public byte bPartType; }

結構體中字符串類型需要指定長度,否則Marshal.SizeOf會報告NotSupportedException
實現

private STOREINFO NewStoreInfo() { STOREINFO si = new STOREINFO(); si.sdi = new STORAGEDEVICEINFO(); si.sdi.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(si.sdi); si.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(si); return si; } private bool NewPartionInfo(ref PARTINFO pi) { pi = new PARTINFO(); pi.cbSize = Marshal.SizeOf(pi); return true; } private string GetFirstStore() { string sd = string.Empty; STOREINFO si = NewStoreInfo(); IntPtr store = FindFirstStore(ref si); if (store != INVALID_HANDLE_VALUE) { IntPtr cstore = OpenStore(si.szDeviceName); if (cstore != INVALID_HANDLE_VALUE) { PARTINFO pi = new PARTINFO(); NewPartionInfo(ref pi); IntPtr part = FindFirstPartition(cstore, ref pi); if (part != INVALID_HANDLE_VALUE) { while (string.IsNullOrEmpty(pi.szVolumeName) && NewPartionInfo(ref pi) && FindNextPartition(part, ref pi)) { } sd = pi.szVolumeName; FindClosePartition(part); } } FindCloseStore(store); } return sd; }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章