我有一个应用程序,可以存储图像和文本等用户数据。我想显示我的应用程序消耗了多少磁盘空间。这可能吗?
最佳答案
我不知道这三个,但我已经做了一些事情来通过文件管理器获取目录中文档的大小,也许这会对您有所帮助,这是代码
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
if (isDir) {
NSPipe *pipe = [NSPipe pipe];
NSTask *t = [[[NSTask alloc] init] autorelease];
[t setLaunchPath:@"/usr/bin/du"];
[t setArguments:[NSArray arrayWithObjects:@"-k", @"-d", @"0", path, nil]];
[t setStandardOutput:pipe];
[t setStandardError:[NSPipe pipe]];
[t launch];
[t waitUntilExit];
NSString *sizeString = [[[NSString alloc] initWithData:[[pipe fileHandleForReading] availableData] encoding:NSASCIIStringEncoding] autorelease];
sizeString = [[sizeString componentsSeparatedByString:@" "] objectAtIndex:0];
bytes = [sizeString longLongValue]*1024;
}
else {
bytes = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] fileSize];
}
字节将返回大小
关于iphone - 如何以编程方式获取应用程序使用的存储状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102407/