当存在存储卡和蓝牙ftp连接时,是否有一种简单的方法可以在Windows Mobile设备上找到存储卡的路径?
安装点通常是"\存储卡",但可以本地化为其他语言或由OEM修改(某些设备使用"\ SD卡"或其他安装点,某些设备支持安装多个存储介质).枚举可用卡的最佳方法是使用FindFirstFlashCard和FindNextFlashCard.
这两个函数都填充了WIN32_FIND_DATA结构.最重要的字段是cFileName,它将包含卡的挂载点的路径(例如"\ Storage Card").
请注意,这些功能也会枚举设备的内部存储器.如果您只关心外部卷,请忽略cFileName为空字符串("")的情况.
使用这些函数需要#include
请记住,"\存储卡"是面向英语的.为不同区域制作的设备可以具有不同的名称.我的设备上存储卡路径的名称因我使用设备的方式而异.
前段时间我在MSDN表单中回答了一些关于如何检测文件系统中存储卡的问题,以及如何获得存储卡的容量.我写了以下内容可能是对这些问题的回答,并认为分享会有所帮助.存储卡在文件系统中显示为临时目录.此程序检查设备根目录中的对象,任何具有temp属性的文件夹都被视为肯定匹配
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); } } } }
我发现使用FindFirstFlashCard/FindNextFlashCard API比枚举目录和检查临时标志(例如将返回蓝牙共享文件夹)更可靠.
以下示例应用程序演示了如何使用它们以及所需的P/Invoke语句.
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); } }