通過研究,我發現可以使用以下平臺特定代碼的依賴服務來確定在iOS、Android和UWP上存盤應用程式檔案的可用空間:
對于 iOS:
private double GetRemainingStorageSpace(string directoryPath)?
{?
var freeExternalStorage = NSFileManager.DefaultManager.GetFileSystemAttributes(directoryPath).FreeSize;
? return (double)freeExternalStorage;
?}
對于Android,它可以像這樣完成:
var path = new StatFs(directoryPath);?long blockSize = path.BlockSizeLong;?
long avaliableBlocks = path.AvailableBlocksLong;?
double freeSpace = blockSize * avaliableBlocks;??
return freeSpace
或像這樣:
var fl = new Java.IO.File(directoryPath);?
var freeSpace = fl.UsableSpace;
return (double)freeSpace;
對于 UWP:
string freeSpaceKey = "System.FreeSpace";??
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(directoryPath);?
var properties = await folder.Properties.RetrievePropertiesAsync(new string[]?{?freeSpaceKey?});??
var freeSpace = properties[freeSpaceKey];??
return (UInt64)freeSpace;
那么我的問題是:
1.) 上面的這些代碼行是否真的回傳了可以存盤在特定目錄中的位元組數?或者它們是否回傳可以存盤在整個設備上的位元組數?
2.)對于Android,我不太確定這兩種方法之間的區別是什么,我希望能解釋一下兩者之間的區別,哪種更好用?
將不勝感激解釋。
uj5u.com熱心網友回復:
1.) 上面的這些代碼行是否真的回傳了可以存盤在特定目錄中的位元組數?或者它們是否回傳可以存盤在整個設備上的位元組數?
顯然是這樣specific directory
,因為在代碼中它需要特定的檔案夾路徑作為引數。
您可以嘗試將directoryPath設定為
System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)
和
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
看看有什么不同。
2.)對于Android,我不太確定這兩種方法之間的區別是什么,我希望能解釋一下兩者之間的區別,哪種更好用?
我不知道有什么區別或者哪個更好,因為根據我的測驗結果是一樣的,關于細節你可以參考這里,它可能會有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/470481.html
標籤:xamarin xamarin.forms xamarin.android xamarin.ios xamarin.uwp
上一篇:從串列視圖中洗掉專案(已解決)