5

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

Thanks, Rick

miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76
Rick
  • 483
  • 1
  • 6
  • 19
  • Still looking for some help... I've tried: NSFileManager.SystemFreeSize.Length but get some nonsense (20) and DriveInfo[] allDrives = DriveInfo.GetDrives(); d.AvailableFreeSpace; But this crashed the app and I'm told by MonoTouch that the API is not supported. Why is it so frigg'n difficult to get the free space on the device???? – Rick Mar 23 '11 at 16:24
  • Still looking for an answer on how to get total free space on the device - Any updates from MonoTouch? – Rick Aug 10 '11 at 21:34

3 Answers3

6

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

Community
  • 1
  • 1
Felix
  • 35,354
  • 13
  • 96
  • 143
0

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;
jmgomez
  • 742
  • 7
  • 13
0

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);
Danny
  • 31
  • 9