问题描述
我一个书面方式Metro风格的应用程序,并要确定承载用户的音乐库的驱动器的可用存储容量。我想禁用某些应用功能,而没有或左磁盘上的空间不大。我使用P / Invoke调用GetDiskFreeSpaceExW并获得错误,并没有有效的字节数。
I am writting a Metro-style app and want to determine the available storage capacity of the drive that hosts the user's music library. I want to disable some app functions while there's no or little space left on the disk. I am Using P/Invoke to call GetDiskFreeSpaceExW and get errors and no valid byte counts.
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetDiskFreeSpaceExW(
string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern int GetLastError();
async static void TestDiskSpace()
{
IStorageFolder musicFolder = KnownFolders.MusicLibrary;
IStorageFolder testFolder = await musicFolder.CreateFolderAsync("test", CreationCollisionOption.OpenIfExists);
IStorageFolder appFolder = ApplicationData.Current.LocalFolder;
ulong a, b, c;
string[] paths = new[]
{
null,
"."
"C:",
"C:\\",
"C:\\Users\\Jonas\\Music",
"C:\\Users\\Jonas\\Music\\",
musicFolder.Path,
testFolder.Path,
appFolder.Path
};
foreach(string path in paths)
{
GetDiskFreeSpaceExW(path, out a, out b, out c);
int error = GetLastError();
Debug.WriteLine(
string.Format("{0} - Error {1} - free = {2}",
path ?? "null", error, a));
}
}
调试输出:
Debug output:
null - Error 5 - free = 0
. - Error 123 - free = 0
C: - Error 3 - free = 0
C:\ - Error 3 - free = 0
C:\Users\J909\Music - Error 3 - free = 0
C:\Users\J909\Music\ - Error 3 - free = 0
- Error 3 - free = 0
C:\Users\J909\Music\test - Error 123 - free = 0
C:\Users\J909\AppData\Local\Packages\long-app-id\LocalState - Error 123 - free = 0
看来我提供了错误的输入。的错误码是3:ERROR_PATH_NOT_FOUND,5:ERROR_ACCESS_DENIED,123:ERROR_INVALID_NAME。我运行在Windows 8 RP(64)在VS终极2012 RC这段代码,从Metro风格的应用程序调用。我的应用程序被授予访问权限的用户的音乐库。
It appears I am providing the wrong input. The error codes are 3: ERROR_PATH_NOT_FOUND, 5: ERROR_ACCESS_DENIED, 123: ERROR_INVALID_NAME. I am running this code on Windows 8 RP (x64) with VS Ultimate 2012 RC, called from a Metro-style app. My app was granted permission to access the user's Music Library.
目前已有人设法从Metro风格应用程序中成功调用这个函数?什么样的目录名被接受,产生的自由空间有效的读数?
Has somebody managed to call this function successfully from within a Metro-style app? What kind of directory name is accepted to produce a valid reading of free space?
推荐答案
我创建了基于此问题的代码 Win32和COM文档地铁。它调用了GetDiskFreeSpaceExW是在地铁可用,但采用p托管的应用程序/调用需要调用GetDiskFreeSpaceEx来代替:
I created the code in this question based on the Win32 and COM documentation for Metro. It calls out GetDiskFreeSpaceExW to be available in metro, but a managed app that uses P/Invoke needs to call GetDiskFreeSpaceEx instead:
[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));
}
这篇关于无法从Metro风格的应用程序获得的可用磁盘空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!