【问题标题】:How can I determine free space on a Windows CE device?如何确定 Windows CE 设备上的可用空间?
【发布时间】:2015-03-14 07:36:08
【问题描述】:

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

我认为 Ken Blanco 的答案 here(与示例 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/compact 框架项目中不可用。

我正在引用 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;
}

...来自here,它可以编译,但我不知道 Windows CE 设备的“文件夹名称”应该是什么;在 Windows 资源管理器中,它根本没有名称。我确定我现在所拥有的(“C:\”)是不正确的......

更新 2

根据“Windows 程序员”here:“如果你运行的是 Windows CE,那么 \ 是根目录

那么,我应该使用:

String folderName = "\";

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

String folderName = "\\";

...或者...???

【问题讨论】:

  • "\" 用于对象存储。像“\Storage Card”或“\USB Disk”这样的挂载媒体。
  • 好的,谢谢;所以我需要逃避那个 (folderName = "\\";) 还是一次打击就足够了?
  • 这是一个字符串。你必须像普通的字符串规则一样转义它,所以你可以这样做 "\\"@"\"

标签: c# compact-framework windows-ce mscorlib driveinfo


【解决方案1】:

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

lp目录名称

[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.
}

【讨论】:

  • MainModule 无法解析。
  • @B.ClayShannon 我已经修改了第一行以使用可以在 Windows CE 上运行的版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 2011-01-26
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
相关资源
最近更新 更多