我想我希望能够找到任何存储,而不仅仅是系统磁盘,但这是最重要的。
7 回答
我用这个:
NSDictionary* fileAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:@"/"
error:&error];
unsigned long long freeSpace = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];
NSLog(@"free disk space: %dGB", (int)(freeSpace / 1073741824));
编辑 fileSystemAttributesAtPath: 已弃用,使用 attributesOfFileSystemForPath:error: 作为 NSD 建议。当我认为它不起作用时,我犯了一个错误。
// this works
NSError *error = nil;
NSDictionary *attr = [NSFM attributesOfFileSystemForPath:@"/" error:&error];
if (!error) {
double bytesFree = [[attr objectForKey:NSFileSystemFreeSize] doubleValue];
}
我试过了, attributesOfItemAtPath:error 但返回的字典似乎没有 NSFileSystemFreeNodes 键。
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSDictionary *attr = [fm attributesOfItemAtPath:@"/" error:&error];
if (!error) {
NSLog(@"Attr: %@", attr);
}
2009-10-28 17:21:11.936 MyApp[33149:a0b] Attr: {
NSFileCreationDate = "2009-08-28 15:37:03 -0400";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 80;
NSFileGroupOwnerAccountName = admin;
NSFileModificationDate = "2009-10-28 15:22:15 -0400";
NSFileOwnerAccountID = 0;
NSFileOwnerAccountName = root;
NSFilePosixPermissions = 1021;
NSFileReferenceCount = 40;
NSFileSize = 1428;
NSFileSystemFileNumber = 2;
NSFileSystemNumber = 234881026;
NSFileType = NSFileTypeDirectory;
}
环顾了一下,似乎 fileSystemAttributesAtPath: 是返回它的方法。奇怪的。
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *attr = [fm fileSystemAttributesAtPath:@"/"];
NSLog(@"Attr: %@", attr);
2009-10-28 17:24:07.993 MyApp[33283:a0b] Attr: {
NSFileSystemFreeNodes = 5027061;
NSFileSystemFreeSize = 20590841856;
NSFileSystemNodes = 69697534;
NSFileSystemNumber = 234881026;
NSFileSystemSize = 285481107456;
}
硬盘设备制造商,使用:
1GB = 1000MB
1MB = 1000 KB 等。
如果您在 Windows 中看到 8GB USB 记忆棒总是显示比实际空间少(例如 7.8 GB),因为它被认为是 1 GB = 1024 MB)。在 OSX 中,相同的 USB 记忆棒是 8GB(真实)。
(freeSpace / 1073741824)
一定是这样(freeSpace / 1000000000)
至少在 OSX 中
通过 swift 您可以使用此功能获得可用空间
func getFreeSpace() -> CGFloat {
do {
let fileAttributes = try NSFileManager.defaultManager().attributesOfFileSystemForPath("/")
if let size = fileAttributes[NSFileSystemFreeSize] as? CGFloat {
return size
}
} catch { }
return 0
}
从这篇文章中获取:
- (uint64_t)freeDiskspace
{
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
__autoreleasing NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %d", [error domain], [error code]);
}
return totalFreeSpace;
}
主要区别在于您应该使用 NSSearchPathForDirectioriesInDomains,否则我在模拟器上得到了正确的值,但在设备上的“/”文件夹引用返回的值类似于 190MB,这是不正确的。
MacOS 上的 Swift 3.0-4.0
我已经为 macOS 完成了此操作,并且没有遇到任何问题。输出以字节为单位,因此您必须将其除以 1024。
func getHD() -> String {
do {
let du = try FileManager.default.attributesOfFileSystem(forPath: "/")
if let size = du[FileAttributeKey.systemFreeSize] as? Int64 {
return (String(size / 1024 / 1024 / 1024) + "GB")
}
debugPrint(du)
}
catch {
debugPrint(error)
return "Check Failed"
}
return "Checking..."
}
这FileManager.default.attributesOfFileSystem(forPath: "/")
只是获取可用于根目录的文件系统的各种属性。其中一个属性恰好是可用磁盘空间。
du[FileAttributeKey.systemFreeSize] as? Int64
您访问该键(键:值)并将其类型转换为 Int 以获得可用磁盘空间。