//获取磁盘总空间大小
-(CGFloat)diskOfAllSizeMBytes{
CGFloat size = 0.0;
NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfItemAtPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"error:%@",error.localizedDescription);
#endif
}else{
NSNumber *number = [dic objectForKey:NSFileSystemSize];
size = [number floatValue]/1024/1024;
}
return size;
}
//获取磁盘可用空间大小
-(CGFloat)diskOfFreeSizeMBytes{
CGFloat size = 0.0;
NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfItemAtPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"error:%@",error.localizedDescription);
#endif
}else{
NSNumber *number = [dic objectForKey:NSFileSystemFreeSize];
size = [number floatValue]/1024/1024;
}
return size;
}
//指定路径下某文件的大小
-(long long)fileSizeAtPath:(NSString *)fileName{
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:fileName]) {
return 0;
}
return [[fileManager attributesOfItemAtPath:fileName error:nil]objectForKey:NSFileSize];
}
//指定文件夹下,所有文件的大小
-(long long)folderSizeAtPath:(NSString *)folderPath
{
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:folderPath]) {
return 0;
}
NSEnumerator *filesEnumerator = [[fileManager subpathsAtPath:folderPath]objectEnumerator];
NSString *fileName;
long long folerSize = 0;
while ((fileName = [filesEnumerator nextObject]) != nil) {
NSString *filePath = [folderPath stringByAppendingString:fileName];
folerSize += [self fileSizeAtPath:filePath];
}
return folerSize;
}