code_junkie

Finding the storage card path on WM6

Nov 10th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace StorageCardInfo
  6. {
  7. class Program
  8. {
  9. const ulong Megabyte = 1048576;
  10. const ulong Gigabyte = 1073741824;
  11.  
  12. [DllImport("CoreDLL")]
  13. static extern int GetDiskFreeSpaceEx(
  14. string DirectoryName,
  15. out ulong lpFreeBytesAvailableToCaller,
  16. out ulong lpTotalNumberOfBytes,
  17. out ulong lpTotalNumberOfFreeBytes
  18. );
  19.  
  20. static void Main(string[] args)
  21. {
  22. DirectoryInfo root = new DirectoryInfo("\");
  23. DirectoryInfo[] directoryList = root.GetDirectories();
  24. ulong FreeBytesAvailable;
  25. ulong TotalCapacity;
  26. ulong TotalFreeBytes;
  27.  
  28. for (int i = 0; i < directoryList.Length; ++i)
  29. {
  30. if ((directoryList.Attributes & FileAttributes.Temporary) != 0)
  31. {
  32. GetDiskFreeSpaceEx(directoryList.FullName, out FreeBytesAvailable, out TotalCapacity, out TotalFreeBytes);
  33. Console.Out.WriteLine("Storage card name: {0}", directoryList.FullName);
  34. Console.Out.WriteLine("Available Bytes : {0}", FreeBytesAvailable);
  35. Console.Out.WriteLine("Total Capacity : {0}", TotalCapacity);
  36. Console.Out.WriteLine("Total Free Bytes : {0}", TotalFreeBytes);
  37. }
  38. }
  39. }
  40. }
  41.  
  42. using System;
  43. using System.Runtime.InteropServices;
  44.  
  45. namespace RemovableStorageTest
  46. {
  47. class Program
  48. {
  49. static void Main(string[] args)
  50. {
  51. string removableDirectory = GetRemovableStorageDirectory();
  52. if (removableDirectory != null)
  53. {
  54. Console.WriteLine(removableDirectory);
  55. }
  56. else
  57. {
  58. Console.WriteLine("No removable drive found");
  59. }
  60. }
  61.  
  62. public static string GetRemovableStorageDirectory()
  63. {
  64. string removableStorageDirectory = null;
  65.  
  66. WIN32_FIND_DATA findData = new WIN32_FIND_DATA();
  67. IntPtr handle = IntPtr.Zero;
  68.  
  69. handle = FindFirstFlashCard(ref findData);
  70.  
  71. if (handle != INVALID_HANDLE_VALUE)
  72. {
  73. do
  74. {
  75. if (!string.IsNullOrEmpty(findData.cFileName))
  76. {
  77. removableStorageDirectory = findData.cFileName;
  78. break;
  79. }
  80. }
  81. while (FindNextFlashCard(handle, ref findData));
  82. FindClose(handle);
  83. }
  84.  
  85. return removableStorageDirectory;
  86. }
  87.  
  88. public static readonly IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
  89.  
  90. // The CharSet must match the CharSet of the corresponding PInvoke signature
  91. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  92. public struct WIN32_FIND_DATA
  93. {
  94. public int dwFileAttributes;
  95. public FILETIME ftCreationTime;
  96. public FILETIME ftLastAccessTime;
  97. public FILETIME ftLastWriteTime;
  98. public int nFileSizeHigh;
  99. public int nFileSizeLow;
  100. public int dwOID;
  101. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  102. public string cFileName;
  103. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  104. public string cAlternateFileName;
  105. }
  106.  
  107. [StructLayout(LayoutKind.Sequential)]
  108. public struct FILETIME
  109. {
  110. public int dwLowDateTime;
  111. public int dwHighDateTime;
  112. };
  113.  
  114. [DllImport("note_prj", EntryPoint = "FindFirstFlashCard")]
  115. public extern static IntPtr FindFirstFlashCard(ref WIN32_FIND_DATA findData);
  116.  
  117. [DllImport("note_prj", EntryPoint = "FindNextFlashCard")]
  118. [return: MarshalAs(UnmanagedType.Bool)]
  119. public extern static bool FindNextFlashCard(IntPtr hFlashCard, ref WIN32_FIND_DATA findData);
  120.  
  121. [DllImport("coredll")]
  122. public static extern bool FindClose(IntPtr hFindFile);
  123. }
  124. }
  125.  
  126. WIN32_FIND_DATA cardinfo;
  127. HANDLE card = FindFirstFlashCard(&cardinfo);
  128. if (card != INVALID_HANDLE_VALUE)
  129. {
  130. TCHAR existFile[MAX_PATH];
  131.  
  132. wprintf(_T("found : %sn"), cardinfo.cFileName);
  133.  
  134. while(FindNextFlashCard(card, &cardinfo))
  135. {
  136. wprintf(_T("found : %sn"), cardinfo.cFileName);
  137. }
  138. }
  139. FindClose(card);
  140.  
  141. cardinfo.dwFileAttributes 0x00000110 unsigned long int
  142. cardinfo.cFileName "Application" wchar_t[260]
  143.  
  144. cardinfo.dwFileAttributes 0x00000110 unsigned long int
  145. cardinfo.cFileName "Cache Disk" wchar_t[260]
  146.  
  147. cardinfo.dwFileAttributes 0x00000110 unsigned long int
  148. cardinfo.cFileName "Storage Card" wchar_t[260]
  149.  
  150. //codesnippet:06EE3DE0-D469-44DD-A15F-D8AF629E4E03
  151. public string GetStorageCardFolder()
  152. {
  153. string storageCardFolder = string.Empty;
  154. foreach (string directory in Directory.GetDirectories("\"))
  155. {
  156. DirectoryInfo dirInfo = new DirectoryInfo(directory);
  157.  
  158. //Storage cards have temporary attributes do a bitwise check.
  159. //http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=612136&SiteID=1
  160. if ((dirInfo.Attributes & FileAttributes.Temporary) == FileAttributes.Temporary)
  161. storageCardFolder = directory;
  162. }
  163.  
  164. return storageCardFolder;
  165. }
  166.  
  167. //
  168. // the storage card is a flash drive mounted as a directory in the root folder
  169. // of the smart device
  170. //
  171. // on english windows mobile systems the storage card is mounted in the directory "/Storage Card",
  172. // if that directory already exists then it's mounted in "/Storage Card2" and so on
  173. //
  174. // the regional name of the mount base dir of the storage card can be found in
  175. // the registry at [HKEY_LOCAL_MACHINESystemStorageManagerProfilesSDMemoryFolder]
  176. //
  177. // in order to find the path of the storage card we look for the flash drive that starts
  178. // with the base name
  179. //
  180.  
  181. public class StorageCard
  182. {
  183. private StorageCard()
  184. {
  185. }
  186.  
  187. public static List<string> GetMountDirs()
  188. {
  189. string key = @"HKEY_LOCAL_MACHINESystemStorageManagerProfilesSDMemory";
  190. string storageCardBaseName = Registry.GetValue(key, "Folder", "Storage Card") as String;
  191. List<string> storageCards = new List<string>();
  192. foreach (string flashCard in GetFlashCardMountDirs())
  193. {
  194. string path = flashCard.Trim();
  195. if (path.StartsWith(storageCardBaseName))
  196. {
  197. storageCards.Add(path);
  198. }
  199. }
  200. return storageCards;
  201. }
  202.  
  203. private static List<string> GetFlashCardMountDirs()
  204. {
  205. List<string> storages = new List<string>();
  206.  
  207. WIN32_FIND_DATA findData = new WIN32_FIND_DATA();
  208. IntPtr handle = IntPtr.Zero;
  209.  
  210. handle = FindFirstFlashCard(ref findData);
  211.  
  212. if (handle != INVALID_HANDLE_VALUE)
  213. {
  214. do
  215. {
  216. if (!string.IsNullOrEmpty(findData.cFileName))
  217. {
  218. storages.Add(findData.cFileName);
  219. storages.Add(findData.cAlternateFileName);
  220. }
  221. }
  222. while (FindNextFlashCard(handle, ref findData));
  223. FindClose(handle);
  224. }
  225.  
  226. return storages;
  227. }
  228.  
  229. private static readonly IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
  230.  
  231. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  232. private struct WIN32_FIND_DATA
  233. {
  234. public int dwFileAttributes;
  235. public FILETIME ftCreationTime;
  236. public FILETIME ftLastAccessTime;
  237. public FILETIME ftLastWriteTime;
  238. public int nFileSizeHigh;
  239. public int nFileSizeLow;
  240. public int dwOID;
  241. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  242. public string cFileName;
  243. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  244. public string cAlternateFileName;
  245. }
  246.  
  247. [StructLayout(LayoutKind.Sequential)]
  248. private struct FILETIME
  249. {
  250. public int dwLowDateTime;
  251. public int dwHighDateTime;
  252. };
  253.  
  254. [DllImport("note_prj", EntryPoint = "FindFirstFlashCard")]
  255. private extern static IntPtr FindFirstFlashCard(ref WIN32_FIND_DATA findData);
  256.  
  257. [DllImport("note_prj", EntryPoint = "FindNextFlashCard")]
  258. [return: MarshalAs(UnmanagedType.Bool)]
  259. private extern static bool FindNextFlashCard(IntPtr hFlashCard, ref WIN32_FIND_DATA findData);
  260.  
  261. [DllImport("coredll")]
  262. private static extern bool FindClose(IntPtr hFindFile);
  263. }
Add Comment
Please, Sign In to add comment