简体   繁体   中英

How to get available space on Storage Device

I'm working on a Windows Store App (Windows 8.1, using VS2012), and I'm having trouble retrieving the available/used space from a specific Storage Folder , which was retrieved from a storage device. Just to be clear, this app is meant to run on a desktop, and exchange files with usb devices connected to it through Storage Device api.

This is what I have until now:

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;

But no success, the 'info' is always an empty array. Any ideas?

I've found out what I was doing wrong. My StorageFolder object is representing a phone connected to the desktop. If one goes through Explorer and access the Phone folder, will see its 'inner folders', which are the actual storage folders from the phone (eg. Internal Storage, SD Card, etc).

The correct way to do this with phones is to access the subfolders (ie. Each internal storage). The code I'm using now:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM