天天看點

查找存儲卡的路徑在WM6 windows mobile

有一种简单的方法来找到在Windows Mobile设备上的存储卡的路径 当有存储卡和蓝牙FTP连接?

本文地址 :CodeGo.net/2314/

------------------------------------------------------------------------------------------------------------------------- 

1.  请记住,“\\存储卡”是英文为主。针对不同的区域做一个设备可能有我的设备上的存储卡路径不同的是如何与我不同的设备。 以前在MSDN表单我回答关于如何检测文件系统的存储卡和一个人如何得到存储卡的容量的几个问题。我写了下面可能是对这些问题的回应,并认为这将是有益的分享。存储卡显示在文件系统临时目录。这个程序会检查设备,并已临时属性被认为是一个积极的匹配任何文件夹的根目录中的对象

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);
   }
  }
 }
}
           

2.  挂载点“\\存储卡”,但可以本地化为其他语言,或由OEM修改(“\\ SD卡”或其他挂载点,和设备支持多种安装到现有的卡 CodeGo.net,最好的方法是FindFirstFlashCard和FindNextFlashCard。 这两个函数填写一个WIN32_FIND_DATA结构。最重要的领域是将包含路径卡的挂载点(如“\\存储卡”)。 需要注意的是该装置的也将通过这些函数。如果你只在乎外在忽略的地方是一个空字符串的情况下(“”)。 使用这些函数需要你的#include <projects.h>和note_prj.lib链接。两者都包含在Windows Mobile的软件开发工具包WM 2000及更高版本。 

3.  我已经在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);
 }
}
           

4.  不能在TreeUK添加和ctacke如下: 这是不能保证找到一个 存储卡-许多设备安装 内置flash灯,在格兰方式,它 会出现在这个名单也是如此。- ctacke 5月8日在18:23 这有 关于HTC和Psion公司运作良好 设备。什么设备,你知道 这是行不通的?将价值 看是否有其他属性 你可以打折的在Flash中构建 内存。-TreeUK 5月9日22:29 要查看关于摩托罗拉MC75(以前是符号),这片(原生)代码的想法:

WIN32_FIND_DATA cardinfo;
HANDLE card = FindFirstFlashCard(&cardinfo);
if (card != INVALID_HANDLE_VALUE)
{
 TCHAR existFile[MAX_PATH];
 wprintf(_T("found : %s\n"), cardinfo.cFileName);
 while(FindNextFlashCard(card, &cardinfo))
 {
  wprintf(_T("found : %s\n"), 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]
           

“应用程序”和“磁盘缓存”是内部闪存驱动器。 “存储卡”可移动SD卡。所有被标记为一个闪存盘上(他们是),但只有“存储卡”是可移动的。 

5.  我张贴在这里,要得到的存储卡安装显示目录的代码。 在那里我得到的闪存卡路径的部分是从Sibly的帖子有一些变化被复制。 主要的区别是在我经历的所有闪存卡装入显示目录搜索和我保持一(s)匹配,我从Windows的注册表中读取默认的存储卡。 它解决了一个人对摩托罗拉的地方有多个闪存卡,只有一个SD卡读卡器,其安装目录的可以从默认的后缀更改(即英文的WM系统智能设备的问题:“存储卡”,“存储卡2 '等)。 我用WM 6.5的英语测试了它在摩托罗拉机型(MC75,MC75A,MC90,MC65)。 这个解决方案应该工作以及与不同的语言,但我不知道它是否能与那些改变处理 默认的存储卡。 这一切都取决于设备制造商是否更新Windows注册表与新的默认 这将是巨大的,如果你可以在不同的窗口管理器或设备进行测试。 反馈

//
 // 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_MACHINE\System\StorageManager\Profiles\SDMemory\Folder]
 // 
 // 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_MACHINE\System\StorageManager\Profiles\SDMemory";
   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);
 }
           

6.  在Windows CE 5(这是基本的Windows Mobile 6)存储卡得到安装在根文件系统为“存储卡\\”,“存储卡2 \\”,等等。 要了解,如果是安装调用GetFileAttributes(或远程版本CeGetFileAttributes我相信)传递的完整路径(“\\存储卡\\”)。如果是那就不是装返回INVALID_FILE_ATTRIBUTES,否则检查,以确保它的返回true之前的目录。 

7.  有一个纯C#的方式来做到这一点没有本机调用。 取自这里。

//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.
  // CodeGo.net 
  if ((dirInfo.Attributes & FileAttributes.Temporary) == FileAttributes.Temporary)
   storageCardFolder = directory;
 }
 return storageCardFolder;
}
           

8.  我一数上面的解决方案,特别是qwlice的代码,发现在多种设备的SD卡。该解决方案发现的SD卡只(所以排除了所有的内部“存储卡”设备有)原生DLL调用。 该代码搜索HKEY_LOCAL_MACHINE \\ SYSTEM \\ StorageManager \\ Profiles文件\\键包含“标清”为上的设备略有不同的键,找到默认的安装目录,然后查找开头为这个临时目录。它会找到\\ StorageCard2,\\ StorageCard3等 我有这样的一个范围和Motorola / Symbol设备,还没有任何问题。下面是下面的代码:

public class StorageCardFinder
{
 public static List<string> GetMountDirs()
 {
  //get default sd card folder name
  string key = @"HKEY_LOCAL_MACHINE\System\StorageManager\Profiles";
  RegistryKey profiles = Registry.LocalMachine.OpenSubKey(@"System\StorageManager\Profiles");
  string sdprofilename = profiles.GetSubKeyNames().FirstOrDefault(k => k.Contains("SD"));
  if (sdprofilename == null)
   return new List<string>();
  key += "\\" + sdprofilename;
  string storageCardBaseName = Registry.GetValue(key, "Folder", "Storage Card") as String;
  if (storageCardBaseName == null)
   return new List<string>();
  //find storage card
  List<string> cardDirectories = GetFlashCardMountDirs();
  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()
 {
  DirectoryInfo root = new DirectoryInfo("\\");
  return root.GetDirectories().Where(d => (d.Attributes & FileAttributes.Temporary) != 0)
         .Select(d => d.Name).ToList();
 }
}
           

本文标题 :查找存储卡的路径在WM6

本文地址 :CodeGo.net/2314/

繼續閱讀