Device free space and sandbox space used

2019-05-01 04:26发布

I'm trying to get the free space on the device and the amount of space my sandbox is using. Any thoughts?

Thanks, Rick

3条回答
孤傲高冷的网名
2楼-- · 2019-05-01 05:14

In Objective-C you can get the available file system space with NSFileManager:

NSFileManager* filemgr = [NSFileManager defaultManager];

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];            

NSDictionary* fsAttr = [filemgr attributesOfFileSystemForPath:docDirectory error:NULL];

unsigned long long freeSize = [(NSNumber*)[fsAttr objectForKey:NSFileSystemFreeSize] unsignedLongLongValue];

To get the amount of space your app is using read this question: Calculate the size of a folder

查看更多
甜甜的少女心
3楼-- · 2019-05-01 05:20

This is the direct translate from @phix23 to MonoTouch (Sorry for the length of the lines, cocoa is too verbose):

var docDir = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomain.User, true)[0];
var freeSpace = NSFileManager.DefaultManager.GetFileSystemAttributes(docDir).FreeSize;
查看更多
做自己的国王
4楼-- · 2019-05-01 05:23

this is close, but not perfect.

NSFileManager a = NSFileManager.DefaultManager;
NSError _error;

string _databaseFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "..");
NSDictionary _b =  a.GetFileSystemAttributes(_databaseFolder,out _error);
NSObject _d =  NSObject.FromObject("NSFileSystemFreeSize");
NSObject _c =  _b.ObjectForKey(_d);
查看更多
登录 后发表回答