Disk space in WinRT using C# in Windows 8

那年仲夏 提交于 2019-12-04 11:22:59

Try this (It's in JavaScript, but should be easy to translate into C#):

var freeSpaceProperty = "System.FreeSpace";
var applicationData = Windows.Storage.ApplicationData.current;
var localFolder = applicationData.localFolder;

localFolder.getBasicPropertiesAsync().then(function (basicProperties) {
    // Get extra properties
    return basicProperties.retrievePropertiesAsync([freeSpaceProperty]);
}).done(function (extraProperties) {
    var propValue = extraProperties[freeSpaceProperty];
    if (propValue !== null) {
        outputDiv.innerText = "Free Space: " + propValue;
}
}, function (error) {
    // Handle errors encountered while retrieving properties
});

You can substitute any other StorageFolder for the appdata folder, e.g. the Music Library folder, as you originally asked.

Here's the C# version of what Kraig said with some code to convert it to string for a good measure:

using System;
using System.Threading.Tasks;
using Windows.Storage;

namespace WinRTXamlToolkit.IO.Extensions
{
    public static class StorageItemExtensions
    {
        public static async Task<UInt64> GetFreeSpace(this IStorageItem sf)
        {
            var properties = await sf.GetBasicPropertiesAsync();
            var filteredProperties = await properties.RetrievePropertiesAsync(new[] { "System.FreeSpace" });
            var freeSpace = filteredProperties["System.FreeSpace"];
            return (UInt64)freeSpace;
        }

        public static string GetSizeString(this ulong sizeInB, double promoteLimit = 1024, double decimalLimit = 10, string separator = " ")
        {
            if (sizeInB < promoteLimit)
                return string.Format("{0}{1}B", sizeInB, separator);

            var sizeInKB = sizeInB / 1024.0;

            if (sizeInKB < decimalLimit)
                return string.Format("{0:F1}{1}KB", sizeInKB, separator);

            if (sizeInKB < promoteLimit)
                return string.Format("{0:F0}{1}KB", sizeInKB, separator);

            var sizeInMB = sizeInKB / 1024.0;

            if (sizeInMB < decimalLimit)
                return string.Format("{0:F1}{1}MB", sizeInMB, separator);

            if (sizeInMB < promoteLimit)
                return string.Format("{0:F0}{1}MB", sizeInMB, separator);

            var sizeInGB = sizeInMB / 1024.0;

            if (sizeInGB < decimalLimit)
                return string.Format("{0:F1}{1}GB", sizeInGB, separator);

            if (sizeInGB < promoteLimit)
                return string.Format("{0:F0}{1}GB", sizeInGB, separator);

            var sizeInTB = sizeInGB / 1024.0;

            if (sizeInTB < decimalLimit)
                return string.Format("{0:F1}{1}TB", sizeInTB, separator);

            return string.Format("{0:F0}{1}TB", sizeInTB, separator);
        }
    }
}

You can use it like that:

var freeSpace = await ApplicationData.Current.LocalFolder.GetFreeSpace();
Debug.WriteLine(freeSpace.GetSizeString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!