2013-10-22 26 views
4

我得到了兩種解決方案,但都對我無用。WinRT在Windows 8中使用C#的磁盤空間

解決方案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

看起來像的複製品http://stackoverflow.com/questions/15415278/get-free-disk-space-in-winrt-using-c-sharp和http://stackoverflow.com/questions/11133084/unable-to-get-free- disk-space-from-metro-style-app – kristianp

+0

是的,我們看到了這個解決方案,但是我們無法在WinRT開發b/c中導入dll文件,這是市場問題tification。 –

回答

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文件夾,例如音樂庫文件夾,就像您最初問到的那樣。

7

這裏是什麼樣的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()); 
+0

非常感謝Filip Skakun。它的工作代碼。 –

+0

感謝您添加Flip! –