确定可用于使用 Xamarin 表单存储应用程序文件的 space 数量的最佳方法?

Best way to determine the amount of space available for storing app files with Xamarin forms?

根据研究,我发现可以确定 space 用于在 iOS[ 上存储应用程序文件=45=]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.) 上面的这些代码行实际上 return 可以存储在特定目录中的字节数吗?或者它们 return 可以存储在整个设备上的字节数?

2.) 对于 Android 我不太确定这两种方法之间的区别是什么我希望能够解释两者之间的区别以及哪种更好使用?

希望得到解释。

1.) Do these lines of code above actually return the amount of bytes that can be stored in a specific directory? Or do they return the amount of bytes that can be stored on the whole device?

显然是specific directory,因为在代码中需要具体的文件夹路径作为参数。

您可以尝试将 directoryPath 设置为 System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

看看区别。


2.) For Android I am not too sure what the difference between the two ways of doing this are I am hoping for an explanation of the difference between the both of then and which is a better to use?

我说不出区别或哪个更好,因为根据我的测试结果是一样的,详细信息您可以参考here,它可能会有所帮助。