我有两个解决方案,但两者对我都没用。
解决方案1:kernel32.dll(其工作代码)
注意:但我不想在我的应用程序中导入任何dll。 b / c它是市场提交的问题。
[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));
}
解决方案2:使用DriveInfo类(WinRT不工作代码)
注意:WinRT开发中缺少名称空间。 WinRT for Windows 8开发不支持此类。
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);
}
}
所以请给我一个不同的解决方案或任何替代方案。
对于Windows 8开发的winrt哪个有用?
答案 0 :(得分:8)
以下是Kraig所说的C#版本,它将一些代码转换为字符串以获得良好的衡量标准:
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);
}
}
}
你可以这样使用它:
var freeSpace = await ApplicationData.Current.LocalFolder.GetFreeSpace();
Debug.WriteLine(freeSpace.GetSizeString());
答案 1 :(得分:5)
尝试这个(它在JavaScript中,但应该很容易转换为C#):
var freeSpaceProperty = "System.FreeSpace";
var applicationData = Windows.Storage.ApplicationData.current;
var localFolder = applicationData.localFolder;
localFolder.getBasicPropertiesAsync().then(function (basicProperties) {
// Get extra properties
return basicProperties.retrievePropertiesAsync([freeSpaceProperty]);
}).done(function (extraProperties) {
var propValue = extraProperties[freeSpaceProperty];
if (propValue !== null) {
outputDiv.innerText = "Free Space: " + propValue;
}
}, function (error) {
// Handle errors encountered while retrieving properties
});
您可以将任何其他StorageFolder替换为appdata文件夹,例如您最初询问的音乐库文件夹。