C# 如何确定Windows CE设备上的可用空间?

C# 如何确定Windows CE设备上的可用空间?,c#,compact-framework,windows-ce,mscorlib,driveinfo,C#,Compact Framework,Windows Ce,Mscorlib,Driveinfo,我需要确定Windows CE设备上有多少可用空间,以有条件地确定是否应继续执行特定操作 我认为Ken Blanco的答案(与示例有惊人的相似之处)会奏效,我将其改编为: internal static bool EnoughStorageSpace(long spaceNeeded) { DriveInfo[] allDrives = DriveInfo.GetDrives(); long freeSpace = 0; foreach (DriveInfo di in

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

我认为Ken Blanco的答案(与示例有惊人的相似之处)会奏效,我将其改编为:

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/compact framework项目中不可用

我引用的是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:\”)是不对的

更新2 根据“Windows程序员”:“如果您运行的是Windows CE,那么\是根目录”

那么,我应该使用:

String folderName = "\";
…还是我需要逃避它:

String folderName = "\\";

…或…?

Windows CE API文档说明了如何使用该函数:

lpDirectoryName

指向以null结尾的字符串的[in]指针,该字符串指定指定磁盘上的目录。此字符串可以是通用命名约定(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.
}

“\”用于对象存储。类似于“\Storage Card”或“\USB Disk”的内容用于装入的媒体。好的,谢谢;那么我需要转义它(folderName=“\\”;)还是一次重击就足够了?它是一个字符串。你必须像字符串的常规规则一样转义它,这样你就可以执行
“\\”
@\”
MainModule无法解决。@B.ClayShannon我已经修改了第一行,使用的版本应该可以在Windows CE上运行。