Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace StorageCardInfo
- {
- class Program
- {
- const ulong Megabyte = 1048576;
- const ulong Gigabyte = 1073741824;
- [DllImport("CoreDLL")]
- static extern int GetDiskFreeSpaceEx(
- string DirectoryName,
- out ulong lpFreeBytesAvailableToCaller,
- out ulong lpTotalNumberOfBytes,
- out ulong lpTotalNumberOfFreeBytes
- );
- static void Main(string[] args)
- {
- DirectoryInfo root = new DirectoryInfo("\");
- DirectoryInfo[] directoryList = root.GetDirectories();
- ulong FreeBytesAvailable;
- ulong TotalCapacity;
- ulong TotalFreeBytes;
- for (int i = 0; i < directoryList.Length; ++i)
- {
- if ((directoryList.Attributes & FileAttributes.Temporary) != 0)
- {
- GetDiskFreeSpaceEx(directoryList.FullName, out FreeBytesAvailable, out TotalCapacity, out TotalFreeBytes);
- Console.Out.WriteLine("Storage card name: {0}", directoryList.FullName);
- Console.Out.WriteLine("Available Bytes : {0}", FreeBytesAvailable);
- Console.Out.WriteLine("Total Capacity : {0}", TotalCapacity);
- Console.Out.WriteLine("Total Free Bytes : {0}", TotalFreeBytes);
- }
- }
- }
- }
- using System;
- using System.Runtime.InteropServices;
- namespace RemovableStorageTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- string removableDirectory = GetRemovableStorageDirectory();
- if (removableDirectory != null)
- {
- Console.WriteLine(removableDirectory);
- }
- else
- {
- Console.WriteLine("No removable drive found");
- }
- }
- public static string GetRemovableStorageDirectory()
- {
- string removableStorageDirectory = null;
- WIN32_FIND_DATA findData = new WIN32_FIND_DATA();
- IntPtr handle = IntPtr.Zero;
- handle = FindFirstFlashCard(ref findData);
- if (handle != INVALID_HANDLE_VALUE)
- {
- do
- {
- if (!string.IsNullOrEmpty(findData.cFileName))
- {
- removableStorageDirectory = findData.cFileName;
- break;
- }
- }
- while (FindNextFlashCard(handle, ref findData));
- FindClose(handle);
- }
- return removableStorageDirectory;
- }
- public static readonly IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
- // The CharSet must match the CharSet of the corresponding PInvoke signature
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
- public struct WIN32_FIND_DATA
- {
- public int dwFileAttributes;
- public FILETIME ftCreationTime;
- public FILETIME ftLastAccessTime;
- public FILETIME ftLastWriteTime;
- public int nFileSizeHigh;
- public int nFileSizeLow;
- public int dwOID;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
- public string cFileName;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
- public string cAlternateFileName;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct FILETIME
- {
- public int dwLowDateTime;
- public int dwHighDateTime;
- };
- [DllImport("note_prj", EntryPoint = "FindFirstFlashCard")]
- public extern static IntPtr FindFirstFlashCard(ref WIN32_FIND_DATA findData);
- [DllImport("note_prj", EntryPoint = "FindNextFlashCard")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool FindNextFlashCard(IntPtr hFlashCard, ref WIN32_FIND_DATA findData);
- [DllImport("coredll")]
- public static extern bool FindClose(IntPtr hFindFile);
- }
- }
- WIN32_FIND_DATA cardinfo;
- HANDLE card = FindFirstFlashCard(&cardinfo);
- if (card != INVALID_HANDLE_VALUE)
- {
- TCHAR existFile[MAX_PATH];
- wprintf(_T("found : %sn"), cardinfo.cFileName);
- while(FindNextFlashCard(card, &cardinfo))
- {
- wprintf(_T("found : %sn"), cardinfo.cFileName);
- }
- }
- FindClose(card);
- cardinfo.dwFileAttributes 0x00000110 unsigned long int
- cardinfo.cFileName "Application" wchar_t[260]
- cardinfo.dwFileAttributes 0x00000110 unsigned long int
- cardinfo.cFileName "Cache Disk" wchar_t[260]
- cardinfo.dwFileAttributes 0x00000110 unsigned long int
- cardinfo.cFileName "Storage Card" wchar_t[260]
- //codesnippet:06EE3DE0-D469-44DD-A15F-D8AF629E4E03
- public string GetStorageCardFolder()
- {
- string storageCardFolder = string.Empty;
- foreach (string directory in Directory.GetDirectories("\"))
- {
- DirectoryInfo dirInfo = new DirectoryInfo(directory);
- //Storage cards have temporary attributes do a bitwise check.
- //http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=612136&SiteID=1
- if ((dirInfo.Attributes & FileAttributes.Temporary) == FileAttributes.Temporary)
- storageCardFolder = directory;
- }
- return storageCardFolder;
- }
- //
- // the storage card is a flash drive mounted as a directory in the root folder
- // of the smart device
- //
- // on english windows mobile systems the storage card is mounted in the directory "/Storage Card",
- // if that directory already exists then it's mounted in "/Storage Card2" and so on
- //
- // the regional name of the mount base dir of the storage card can be found in
- // the registry at [HKEY_LOCAL_MACHINESystemStorageManagerProfilesSDMemoryFolder]
- //
- // in order to find the path of the storage card we look for the flash drive that starts
- // with the base name
- //
- public class StorageCard
- {
- private StorageCard()
- {
- }
- public static List<string> GetMountDirs()
- {
- string key = @"HKEY_LOCAL_MACHINESystemStorageManagerProfilesSDMemory";
- string storageCardBaseName = Registry.GetValue(key, "Folder", "Storage Card") as String;
- List<string> storageCards = new List<string>();
- foreach (string flashCard in GetFlashCardMountDirs())
- {
- string path = flashCard.Trim();
- if (path.StartsWith(storageCardBaseName))
- {
- storageCards.Add(path);
- }
- }
- return storageCards;
- }
- private static List<string> GetFlashCardMountDirs()
- {
- List<string> storages = new List<string>();
- WIN32_FIND_DATA findData = new WIN32_FIND_DATA();
- IntPtr handle = IntPtr.Zero;
- handle = FindFirstFlashCard(ref findData);
- if (handle != INVALID_HANDLE_VALUE)
- {
- do
- {
- if (!string.IsNullOrEmpty(findData.cFileName))
- {
- storages.Add(findData.cFileName);
- storages.Add(findData.cAlternateFileName);
- }
- }
- while (FindNextFlashCard(handle, ref findData));
- FindClose(handle);
- }
- return storages;
- }
- private static readonly IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
- private struct WIN32_FIND_DATA
- {
- public int dwFileAttributes;
- public FILETIME ftCreationTime;
- public FILETIME ftLastAccessTime;
- public FILETIME ftLastWriteTime;
- public int nFileSizeHigh;
- public int nFileSizeLow;
- public int dwOID;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
- public string cFileName;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
- public string cAlternateFileName;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct FILETIME
- {
- public int dwLowDateTime;
- public int dwHighDateTime;
- };
- [DllImport("note_prj", EntryPoint = "FindFirstFlashCard")]
- private extern static IntPtr FindFirstFlashCard(ref WIN32_FIND_DATA findData);
- [DllImport("note_prj", EntryPoint = "FindNextFlashCard")]
- [return: MarshalAs(UnmanagedType.Bool)]
- private extern static bool FindNextFlashCard(IntPtr hFlashCard, ref WIN32_FIND_DATA findData);
- [DllImport("coredll")]
- private static extern bool FindClose(IntPtr hFindFile);
- }
Add Comment
Please, Sign In to add comment