2015-06-10 35 views
0

我有一个mac应用程序。如何以编程方式获取mac的存储容量?

enter image description here

如截图所示,我想在我的Mac OS X

可可应用程序获取此我想要得到的总容量,可用空间和自由空间。

任何人都可以帮助我吗?

谢谢...

+0

statfs(2)for C-based solution。 https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/statfs.2.html – user3125367

回答

1

我从link得到了我的答案。

所以我张贴我所做的使用该参考。

NSError *error; 

    NSFileManager *fm = [NSFileManager defaultManager]; 
    NSDictionary *attr = [fm attributesOfFileSystemForPath:@"/" 
                error:&error]; 
    NSLog(@"Attr: %@", attr); 
    float totalsizeGb = [[attr objectForKey:NSFileSystemSize]floatValue]/1000000000; 
    NSLog(@" size in GB %.2f",totalsizeGb); 

    float freesizeGb = [[attr objectForKey:NSFileSystemFreeSize]floatValue]/1000000000; 
    NSLog(@" size in GB %.2f",freesizeGb); 

希望能帮助其他人。

谢谢...

4

使用-[NSFileManager mountedVolumeURLsIncludingResourceValuesForKeys:options:]获得NSURL S为卷。对于propertyKeys,请使用@[ NSURLVolumeTotalCapacityKey, NSURLVolumeAvailableCapacityKey ]。您可能想在选项中使用NSVolumeEnumerationSkipHiddenVolumes

然后,对于每个URL,请使用相同的属性键调用-[NSURL resourceValuesForKeys:error:]。这将为您提供一个字典,其关键字为NSURLVolumeTotalCapacityKeyNSURLVolumeAvailableCapacityKey,其值为NSNumber保存相应数量的对象(以字节为单位)。

如果您需要格式化这些值以供显示,请使用NSByteCountFormatter

+0

感谢您的回复。你能告诉我通过代码获取mac的总容量,因为我对mac开发稍微陌生。谢谢... – Manthan

+0

这是我使用NSURL和NSByteCountFormatter的部分解决方案:\t 'NSDictionary * resources = [url resourceValuesForKeys:keys error:&error]; NSLog(@“NSURL resources:%@”,resources); 如果([URL getResourceValue:&availableSpace forKey:NSURLVolumeAvailableCapacityKey误差:&错误] == YES){ \t的NSString * formattedAvailableSpace = [NSByteCountFormatter stringFromByteCount:[availableSpace longLongValue] countStyle:NSByteCountFormatterCountStyleFile]; NSLog(@“availableSpace:%@ formattedAvailableSpace:%@”,availableSpace,formattedAvailableSpace); \t NSLog } ' – edenwaith

相关问题