如何使用 Xamarin IOS 获取免费内部存储 space
How to get free internal storage space using Xamarin IOS
我可以使用 (NSProcessInfo.ProcessInfo.PhysicalMemory) 获取 RAM 详细信息。但我想使用 Xamarin IOS 获得免费的内部设备存储。
获取内部免费space的方法是这个:
NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
如果您正在使用Xamarin.Forms
,您可以制作自定义界面
namespace Your.Namespace.Interfaces
{
public interface IStorageInterface
{
double GetFreeSpace(); //Not sure about the return type, try long, or double
}
}
在您的 iOS 项目中:
[assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.iOS.StorageRenderer))]
namespace Your.Namespace.iOS
{
public class StorageRenderer : IStorageInterface
{
public double GetFreeSpace()
{
return NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
}
}
}
我已经将你的代码转换成Xamarin.iOS如下:
private ulong GetFreeDiskspace()
{
ulong totalSpace = 0;
ulong totalFreeSpace = 0;
NSError error = null;
string[] paths = NSSearchPath.GetDirectories(NSSearchPathDirectory.UserDirectory, NSSearchPathDomain.All);
var defManager = NSFileManager.DefaultManager;
var dicAttributes = defManager.
GetFileSystemAttributes(paths.First()
, out error);
totalSpace = dicAttributes.Size;
totalFreeSpace = dicAttributes.FreeSize;
return totalFreeSpace;
}
祝你好运!
如有疑问回复!
我可以使用 (NSProcessInfo.ProcessInfo.PhysicalMemory) 获取 RAM 详细信息。但我想使用 Xamarin IOS 获得免费的内部设备存储。
获取内部免费space的方法是这个:
NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
如果您正在使用Xamarin.Forms
,您可以制作自定义界面
namespace Your.Namespace.Interfaces
{
public interface IStorageInterface
{
double GetFreeSpace(); //Not sure about the return type, try long, or double
}
}
在您的 iOS 项目中:
[assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.iOS.StorageRenderer))]
namespace Your.Namespace.iOS
{
public class StorageRenderer : IStorageInterface
{
public double GetFreeSpace()
{
return NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
}
}
}
我已经将你的代码转换成Xamarin.iOS如下:
private ulong GetFreeDiskspace()
{
ulong totalSpace = 0;
ulong totalFreeSpace = 0;
NSError error = null;
string[] paths = NSSearchPath.GetDirectories(NSSearchPathDirectory.UserDirectory, NSSearchPathDomain.All);
var defManager = NSFileManager.DefaultManager;
var dicAttributes = defManager.
GetFileSystemAttributes(paths.First()
, out error);
totalSpace = dicAttributes.Size;
totalFreeSpace = dicAttributes.FreeSize;
return totalFreeSpace;
}
祝你好运!
如有疑问回复!