使用C#在Windows 8中WinRT的磁盘空间

4

我有两个解决方案,但都对我没有用。

解决方案1:kernel32.dll(它的工作代码)

注意:但我不想在我的应用程序中导入任何dll。因为这会影响市场上的提交。

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetDiskFreeSpaceEx(
    string lpDirectoryName,
    out ulong lpFreeBytesAvailable,
    out ulong lpTotalNumberOfBytes,
    out ulong lpTotalNumberOfFreeBytes);


static void TestDiskSpace()
{
    IStorageFolder appFolder = ApplicationData.Current.LocalFolder;
    ulong a, b, c;
    if(GetDiskFreeSpaceEx(appFolder.Path, out a, out b, out c))
        Debug.WriteLine(string.Format("{0} bytes free", a));
}

解决方案2:使用DriveInfo类(WinRT不适用)

注意:在WinRT开发中,命名空间丢失。这个类不支持Windows 8开发中的WinRT。

DriveInfo[] allDrives = DriveInfo.GetDrives();

    foreach (DriveInfo d in allDrives)
    {
        Console.WriteLine("Drive {0}", d.Name);
        Console.WriteLine("  File type: {0}", d.DriveType);
        if (d.IsReady == true)
        {
            Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
            Console.WriteLine("  File system: {0}", d.DriveFormat);
            Console.WriteLine(
                "  Available space to current user:{0, 15} bytes", 
                d.AvailableFreeSpace);

            Console.WriteLine(
                "  Total available space:          {0, 15} bytes",
                d.TotalFreeSpace);

            Console.WriteLine(
                "  Total size of drive:            {0, 15} bytes ",
                d.TotalSize);
        }
    }

请提供不同的解决方案或任何替代方案。

对于Windows 8开发,哪个是有用的WinRT?


看起来是 https://dev59.com/dm_Xa4cB1Zd3GeqP3qkB 和 http://stackoverflow.com/questions/11133084/unable-to-get-free-disk-space-from-metro-style-app 的重复。 - kristianp
是的,我们看到了那个解决方案,但是由于市场认证的问题,我们在WinRT开发中无法导入dll文件。 - CB Yadav
2个回答

8

以下是Kraig说的C#版本,加上一些代码将其转换为字符串:

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);
        }
    }
}

您可以像这样使用它:

var freeSpace = await ApplicationData.Current.LocalFolder.GetFreeSpace();
Debug.WriteLine(freeSpace.GetSizeString());

感谢您添加了那个Flip! - Kraig Brockschmidt - MSFT

5

尝试这个(它是使用JavaScript编写的,但应该容易翻译为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
});

你可以将任何其他StorageFolder替换为appdata文件夹,例如你最初提出的音乐库文件夹。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接