此代码正确但文件夹大小错误。如果我更改目录,则大小总是错误的。例如,“%@ / Chat / com.apple.Safari /网页预览”的大小为23 MB,但我有16.5 KB。
NSString *path = [NSString stringWithFormat:@"%@/Caches/com.apple.Safari/Webpage Previews", [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
NSNumber *fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] objectForKey:NSFileSize];
resultsize += [fileSize unsignedLongLongValue];
我也用过这个,但尺码总是错的:
NSFileManager *fm = [[NSFileManager alloc] init];
NSURL *LibraryURL = [[fm URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *previewsURL = [LibraryURL URLByAppendingPathComponent:@"Caches/com.apple.Safari/Webpage Previews"];
resultSize += [[[fm attributesOfItemAtPath:[previewsURL path] error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
你能帮帮我吗?
感谢。
答案 0 :(得分:4)
如果要获取文件夹中所有文件的大小,则需要遍历文件夹的内容并获取实际文件的大小:
unsigned long long totalSize = 0;
NSFileManager *fm = [[NSFileManager alloc] init];
NSURL *libraryURL = [[fm URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *previewsURL = [LibraryURL URLByAppendingPathComponent:@"Caches/com.apple.Safari/Webpage Previews"];
NSDirectoryEnumerator *enumerator = [fm enumeratorAtURL:previewsURL includingPropertiesForKeys:[NSArray arrayWithObject:NSURLFileSizeKey] options:0 errorHandler:nil /* or an actual error handler */];
for (NSURL *url in enumerator) {
NSNumber *sizeNumber;
if ([url getResourceValue:&sizeNumber forKey:NSURLFileSizeKey error:nil /* or an error */])
totalSize += [sizeNumber unsignedLongLongValue];
}
(我没有测试过这个。)
答案 1 :(得分:-1)
Lion为这些累积大小提供了某种缓存。试用服务器卷(Mac OS X Lion Server AFP):
- >这些信息存储在哪里?要使用哪种API?