【问题标题】:How to get available space on Storage Device如何获取存储设备上的可用空间
【发布时间】:2015-04-01 12:44:01
【问题描述】:

我正在开发一个 Windows 应用商店应用程序(Windows 8.1,使用 VS2012),并且我在从特定存储文件夹检索可用/已用空间时遇到问题,该存储文件夹是从储存设备。需要说明的是,此应用程序旨在在桌面上运行,并通过 Storage Device api 与连接到它的 USB 设备交换文件。

这是我到现在为止的:

StorageFolder folder = Windows.Devices.Portable.StorageDevice.FromId(phoneId);

UInt64[] info = new UInt64[] {};
var properties = await folder.Properties.RetrievePropertiesAsync(
    new string[] { "System.FreeSpace", "System.Capacity" });

if (properties.ContainsKey("System.FreeSpace") && properties.ContainsKey("System.FreeSpace"))
{
  info = new UInt64[] {
    (UInt64) properties["System.FreeSpace"],
    (UInt64) properties["System.Capacity"]
  };
}
return info;

但没有成功,“信息”始终是一个空数组。有什么想法吗?

【问题讨论】:

标签: c# windows-store-apps storage


【解决方案1】:

我发现我做错了什么。我的 StorageFolder 对象代表连接到桌面的电话。如果通过资源管理器访问手机文件夹,将看到其“内部文件夹”,即手机中的实际存储文件夹(例如,内部存储、SD 卡等)。

使用手机执行此操作的正确方法是访问子文件夹(即每个内部存储)。我现在使用的代码:

StorageFolder folder = Windows.Devices.Portable.StorageDevice.FromId(phoneId);

var data = new List<Tuple<string, UInt64[]>> { };
IReadOnlyList<StorageFolder> subFolders = await folder.GetFoldersAsync();
foreach (StorageFolder subFolder in subFolders)
{
  var props = await subFolder.Properties.RetrievePropertiesAsync(
    new string[] { "System.FreeSpace", "System.Capacity" });
  if (props.ContainsKey("System.FreeSpace") && props.ContainsKey("System.Capacity"))
  {
    data.Add(Tuple.Create(
      subFolder.Name,
      new UInt64[] {
        (UInt64) props["System.FreeSpace"],
        (UInt64) props["System.Capacity"]
    }));
  }
}
return data;

【讨论】:

    猜你喜欢
    • 2019-07-09
    • 2020-11-03
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多