开发项目时,经常会遇到,要将文件保存到存储卡上的需求。如何得到存储卡的位置?存储卡空间还有多少?有那些外置存储卡呢? 也许你曾经头疼,但现在,下面这个CLASS将对你有极大的帮助! using System; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace BAGUWEN { public class DeviceMemoryInfo { /// <summary> /// 判断是否存在存储卡 /// </summary> public static bool HasStorageCardPresent { get { return DeviceMemoryInfo.IsStorageCard(); } } /// <summary> /// 存储卡目录名(数组)。 /// </summary> public static string[] StorageCardNames { get { return DeviceMemoryInfo.GetStorageCardNames(); } } /// <summary> /// 存储卡目录名(列表)。 /// </summary> public static string StorageCardNameList { get { return DeviceMemoryInfo.GetStorageCardList(); } } private static long freeBytesAvailable = 0; /// <summary> /// 判断当前可用内存,单位为Bytes /// </summary> public static long FreeBytesAvailable { get { DeviceMemoryInfo.GetStoreageSize(); return freeBytesAvailable; } } private static long totalBytes = 0; /// <summary> /// 判断当前总共内存数量,单位为Bytes /// </summary> public static long TotalBytes { get { DeviceMemoryInfo.GetStoreageSize(); return totalBytes; } } private static long totalFreeBytes = 0; /// <summary> /// 判断当前剩余内存,单位Bytes /// </summary> public static long TotalFreeBytes { get { DeviceMemoryInfo.GetStoreageSize(); return totalFreeBytes; } } static string directoryName = string.Empty; /// <summary> /// 设定需要计算容量的目录。默认为 windows 目录。 /// </summary> public static string DirectoryName { get { return directoryName; } set { directoryName = value; } } [DllImport("coredll")] private static extern bool GetDiskFreeSpaceEx(string directoryName, ref long freeBytesAvailable, ref long totalBytes, ref long totalFreeBytes); private static void GetStoreageSize() { DiskFreeSpace result = new DiskFreeSpace(); if (string.IsNullOrEmpty(directoryName)) { directoryName = @"/Windows"; } if (!GetDiskFreeSpaceEx(directoryName, ref result.FreeBytesAvailable, ref result.TotalBytes, ref result.TotalFreeBytes)) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Error retrieving free disk space"); } else { freeBytesAvailable = result.FreeBytesAvailable; totalBytes = result.TotalBytes; totalFreeBytes = result.TotalFreeBytes; } } private struct DiskFreeSpace { public long FreeBytesAvailable; public long TotalBytes; public long TotalFreeBytes; } private static bool IsStorageCard() { bool hasStorageCard = false; DirectoryInfo storeageCard = new DirectoryInfo(@"/"); foreach (DirectoryInfo directory in storeageCard.GetDirectories()) { if (directory.Attributes == (FileAttributes.Temporary | FileAttributes.Directory)) { hasStorageCard = true; break; } else { return hasStorageCard = false; } } return hasStorageCard; } private static string[] GetStorageCardNames() { StringBuilder folderName = new StringBuilder(1000); DirectoryInfo storeageCard = new DirectoryInfo(@"/"); foreach (DirectoryInfo directory in storeageCard.GetDirectories()) { if (directory.Attributes == (FileAttributes.Temporary | FileAttributes.Directory)) { folderName.Append(directory.FullName); folderName.Append("<"); } } return folderName.ToString().Split('<'); } private static string GetStorageCardList() { StringBuilder folderName = new StringBuilder(1000); DirectoryInfo storeageCard = new DirectoryInfo(@"/"); foreach (DirectoryInfo directory in storeageCard.GetDirectories()) { if (directory.Attributes == (FileAttributes.Temporary | FileAttributes.Directory)) { folderName.Append(directory.FullName); folderName.Append("<"); } } return folderName.ToString(); } } }