Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disk space in WinRT using C# in Windows 8

I got two solutions but both are not useful for me.

Solution 1: kernel32.dll (its working code)

Note: But I don’t want to import any dll in my application. b/c its problem with market place submission.

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

Solution 2: Using DriveInfo Class (Not Working Code for WinRT)

Note: namespace is missing in WinRT development. This class is not supported in WinRT for windows 8 development.

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

So please provide me a different solution or any alternative.

Which is useful for winrt for windows 8 development?

like image 206
CB Yadav Avatar asked Dec 12 '22 10:12

CB Yadav


1 Answers

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());
like image 134
Filip Skakun Avatar answered Dec 23 '22 16:12

Filip Skakun