2

我需要确定 Windows CE 设备上有多少可用空间,以有条件地确定是否应继续执行特定操作。

我认为肯布兰科在这里的回答(与上面的例子有惊人的相似之处)会起作用,我将其改编为:

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 设备的“文件夹名称”应该是什么;在 Windows 资源管理器中,它根本没有名称。我确定我现在所拥有的(“C:\”)是不正确的......

更新 2

根据此处的“Windows 程序员” :“如果您运行的是 Windows CE,则 \ 是根目录

那么,我应该使用:

String folderName = "\";

...还是我需要逃避它:

String folderName = "\\";

...或者...???

4

1 回答 1

4

Windows CE API 文档解释了如何使用该函数:http: //msdn.microsoft.com/en-us/library/ms890887.aspx

lp目录名称

[在] 指向指定磁盘上的目录的以空字符结尾的字符串的指针。此字符串可以是通用命名约定 (UNC) 名称。

如果 lpDirectoryName 为 NULL,GetDiskFreeSpaceEx 函数获取有关对象存储的信息。注意 lpDirectoryName 不必指定磁盘上的根目录。该函数接受磁盘上的任何目录。

Windows CE 不使用驱动器号,而是文件系统是一个统一的树,就像在 Linux 上一样,它可以由实际不存在的目录组成,或者父目录的子目录可以存在于不同的物理卷上(或甚至可能根本没有传统卷:CE 支持将 ROM 和 RAM 卷与传统闪存存储合并,所有这些都在同一个文件系统树中)。

假设您的设备将多个卷合并到一个树中,我们仍然可以假设您的应用程序的目录将位于一个卷上,并且您感兴趣的是这个卷,在这种情况下,此代码将适合您:

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.
}
于 2015-01-15T19:59:25.873 回答