locked
Disk space in WinRT using C# in Windows 8. RRS feed

  • Question

  • 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.

    [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?

    Thanks in Advance,

    CB Yadav


    • Edited by CB Yadav Tuesday, October 22, 2013 6:39 AM
    Tuesday, October 22, 2013 6:36 AM

Answers

  • I got the Solution 
    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);
            }
        }
    }
    Use this code by :
    var freeSpace = await ApplicationData.Current.LocalFolder.GetFreeSpace();
    Debug.WriteLine(freeSpace.GetSizeString());
    Code is working.

    CB

    • Marked as answer by CB Yadav Friday, October 25, 2013 7:26 AM
    Friday, October 25, 2013 7:26 AM
  • Unfortunately, you can not access that information from C# without writing a C++ component. More info here.
    Tuesday, October 22, 2013 7:09 AM

All replies

  • Unfortunately, you can not access that information from C# without writing a C++ component. More info here.
    Tuesday, October 22, 2013 7:09 AM
  • Hello Krols,

    Is there any another class, same like DriveInfo which we can use for WinRT development for windows 8. My problem is only get the free space.inside the drive.


    CB

    Tuesday, October 22, 2013 7:53 AM
  • I got the Solution 
    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);
            }
        }
    }
    Use this code by :
    var freeSpace = await ApplicationData.Current.LocalFolder.GetFreeSpace();
    Debug.WriteLine(freeSpace.GetSizeString());
    Code is working.

    CB

    • Marked as answer by CB Yadav Friday, October 25, 2013 7:26 AM
    Friday, October 25, 2013 7:26 AM