3
        [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'm developing a Windows Store App. Why a variable contains 0 when I call:

GetFreeDiskSpace("C:\\");

?

The line with Debug.WriteLine(a) isn't executed.

svick
  • 236,525
  • 50
  • 385
  • 514
Aleff
  • 257
  • 1
  • 3
  • 13

2 Answers2

1

Researching something else I ended up finding the answer: "In Windows 8 Metro Apps you are not allowed to access folders or drive outside of the KnownFolders."

MSDN

Aleff
  • 257
  • 1
  • 3
  • 13
  • 1
    I use filepicker to get access to the files and I do. But when I run the function for getting free space the only folder which seems to work is `ApplicationData.Current.LocalFolder` – Alireza Noori Jul 10 '13 at 13:27
0

You are writing the drive wrong. It needs to be this:

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. It is different on 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));
}
Community
  • 1
  • 1
Kirk Backus
  • 4,776
  • 4
  • 32
  • 52
  • I do this before and the lpFreeBytesAvaliable variable (called 'a' in my method) is already 0. – Aleff Mar 14 '13 at 16:54
  • I missed that you were writing for WinRT, which does it differently. – Kirk Backus Mar 14 '13 at 16:56
  • it's as if my application does not have permission to access the disk :( – Aleff Mar 14 '13 at 16:56
  • ApplicationData.Current.LocalFolder is the local app data store. I need the avaliable space of the disk. – Aleff Mar 14 '13 at 17:00
  • That will give you the total free space on the disk. Or at least the disk that contains that folder. It doesn't matter what folder you are in, since the free space will always be unchanged. – Kirk Backus Mar 14 '13 at 17:23
  • For me is the same scenario both case. I try get free space at least disck that contains some folder choosed by the user, but 'a' variable is 0, so I user "c://" for test but 'a' is zero again :/ – Aleff Mar 14 '13 at 17:27
  • 1
    "Metro applications do not have access to the file system except for the application storage folders (local and roaming) and the known folders (if the appropriate capability is entered in the application manifest)." – Aleff Mar 15 '13 at 20:25
  • @Aleff So isn't there ANY way to get this info for disks? Why have Microsoft people allow `GetDiskFreeSpaceEx` if they were going to restrict this....? I would really appreciate any help. – Alireza Noori Jul 10 '13 at 13:31
  • @Alieza Unfortunately is a limitation of the Windows Store Apps. It's not possible get acess from other disks :( I suffered a lot with this problem, as my problem was to writing files, I decided to use another method. Expect the IOException to be raised and gave the HResult showing a MessageDialog to the user. – Aleff Jul 17 '13 at 10:55
  • See: http://stackoverflow.com/questions/9293227/how-to-check-if-ioexception-is-not-enough-disk-space-exception-type Hope I have helped. – Aleff Jul 17 '13 at 11:04