[英]How can I determine free space on a Windows CE device?
我需要確定Windows CE設備上有多少可用空間,以有條件地確定是否應執行特定操作。
我認為Ken Blanco 在這里的答案(與yonder示例非常相似)可以起作用,我將其改編為:
internal static bool EnoughStorageSpace(long spaceNeeded)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
long freeSpace = 0;
foreach (DriveInfo di in allDrives)
{
if (di.IsReady)
{
freeSpace = di.AvailableFreeSpace;
}
}
return freeSpace >= spaceNeeded;
}
...但是DriveInfo在我的Windows CE /緊湊框架項目中不可用。
我正在引用mscorlib,並且正在使用System.IO,但是由於DriveInfo比我的編輯器中的堪薩斯城酋長隊球衣更紅,因此我認為它不可用。
有沒有另一種方法可以完成同一件事?
我修改了這個:
[DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
public static bool EnoughStorageSpace(ulong freespaceNeeded)
{
String folderName = "C:\\";
ulong freespace = 0;
if (string.IsNullOrEmpty(folderName))
{
throw new ArgumentNullException("folderName");
}
ulong free, dummy1, dummy2;
if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2))
{
freespace = free;
}
return freespace >= freespaceNeeded;
}
...從這里編譯,但是我不知道Windows CE設備的“ folderName”應該是什么; 在Windows資源管理器中,它根本沒有名稱。 我敢肯定我現在所擁有的(“ C:\\”)是不正確的...
根據此處的 “ Windows程序員”的說法:“ 如果您正在運行Windows CE,則\\是根目錄 ”
因此,我應該使用:
String folderName = "\";
...或者我需要對其進行轉義:
String folderName = "\\";
...要么...???
Windows CE API文檔說明了如何使用該功能: http : //msdn.microsoft.com/zh-cn/library/ms890887.aspx
lpDirectoryName
[in]指向以空值結尾的字符串的指針,該字符串指定指定磁盤上的目錄。 該字符串可以是通用命名約定(UNC)名稱。
如果lpDirectoryName為NULL,則GetDiskFreeSpaceEx函數將獲取有關對象存儲的信息。 注意lpDirectoryName不必指定磁盤上的根目錄。 該函數接受磁盤上的任何目錄。
Windows CE不使用驅動器字母,而是文件系統是一個統一樹,就像在Linux上一樣,可以由實際上不存在的目錄組成,或者父目錄的子目錄可以存在於不同的物理卷上(或也許甚至根本沒有傳統的卷:CE支持將ROM和RAM卷與傳統的Flash存儲合並,它們都在同一文件系統樹中)。
假設您的設備將多個卷組合到一個樹中,我們仍然可以假設您的應用程序目錄位於單個卷上,並且您感興趣的就是該卷,在這種情況下,此代碼適合您:
String executingFileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
String executingDirectory = System.IO.Path.GetDirectoryName( executingFileName );
UInt64 userFreeBytes, totalDiskBytes, totalFreeBytes;
if( GetDiskFreeSpaceEx( executingDirectory, out userFreeBytes, out totalDiskBytes, totalFreeBytes ) {
// `userFreeBytes` is the number of bytes available for your program to write to on the mounted volume that contains your application code.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.