Get free disk space in WinRT using C#

放肆的年华 提交于 2019-12-10 19:06:19

问题


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


回答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




回答2:


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


来源:https://stackoverflow.com/questions/15415278/get-free-disk-space-in-winrt-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!