Get free disk space in WinRT using C #

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

        // Returns free disk space from directory.
        public static ulong GetFreeDiskSpace(string directory)
        {
            ulong a, b, c;

            if (GetDiskFreeSpaceEx(directory, out a, out b, out c))
            {
                Debug.WriteLine(a);
            }


            return a;
        }

I am developing an application for the Windows Store. Why does the variable contain 0 when I call:

GetFreeDiskSpace("C:\\");

?

A line with Debug.WriteLine (a) is not executed.

+3
source share
2 answers

Having studied something else, I found the answer: "In Windows 8 Metro Apps, you are not allowed to access folders or go beyond KnownFolders."

MSDN

+1
source

You are writing the disc incorrectly. It should be the following:

GetFreeDiskSpace("C:");

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

Also found this on another page. This is different from WinRT.

Unable to get free disk space from Metro-style app

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

All Articles