2009-10-26 77 views

回答

13
+0

获得免费空间。 – zekel 2009-10-28 21:26:48

+1

哎呀,你是对的。看起来我在课上试过,而不是实例。 – zekel 2010-01-20 22:06:12

+0

嗨,如果路径中的某个子文件夹指向另一个文件系统,NSFileManager是否知道在检索属性时忽略它? – Zohar81 2018-01-18 10:12:43

9

编辑 fileSystemAttributesAtPath:已过时,使用attributesOfFileSystemForPath:错误:为NSD建议。当我认为它不起作用时,我犯了一个错误。

// this works 
NSError *error = nil; 
NSDictionary *attr = [NSFM attributesOfFileSystemForPath:@"/" error:&error]; 
if (!error) { 
    double bytesFree = [[attr objectForKey:NSFileSystemFreeSize] doubleValue]; 
} 

我想这一点,attributesOfItemAtPath:错误,但字典返回似乎不具备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; 
} 

looking around a bit后,好像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; 
} 
+0

“...使用attributesOfFileSystemForPath:error:instead。”这就是NSD的建议,并且你告诉他它不起作用。它现在工作吗? – 2010-01-20 20:47:18

+0

是的,它的确如此。看起来我以前犯过一个错误。 – zekel 2010-01-20 22:09:11

+2

两个批评:'错误:'参数需要一个指向对象的指针,'NSError **'。因此,正确的空指针常量不是“nil”(它可能是指向对象的指针,或者是“ *”或“id”),它是NULL。更重要的是,不要抑制错误return-pass在那里指向一个变量,并且在检索属性失败的情况下(并且仅在)时报告错误。沉默失败是不好的。 – 2010-01-21 02:11:05

7

我用这个:

NSDictionary* fileAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:@"/" 
                        error:&error]; 
unsigned long long freeSpace = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue]; 
NSLog(@"free disk space: %dGB", (int)(freeSpace/1073741824)); 
+0

完美的代码!像魅力一样工作!非常感谢分享。 :) – zeFree 2013-03-01 15:51:26

+0

我的2美分 - 1)使用包含指向卷的路径的NSString来代替@“/”来查看其他卷的信息。 2)如果我们使用unsignedLongLongValue而不是longLongValue,会更好。 – zeFree 2013-03-02 08:27:07

-1

硬盘设备制造商,使用:

1GB = 1000MB

1MB = 1000 KB 等

如果你看到Windows中的8GB USB记忆棒总是显示比真实(如7.8 GB)更小的空间,因为它被认为是1GB = 1024MB)。在OSX中,相同的USB存储棒是8GB(实际)。

所以(freeSpace/1073741824)必须(freeSpace/1000000000)

至少在OSX

+0

对于谁有downvote,我邀请看giga,mega等的意思的希腊词汇。 – Mike97 2016-04-22 16:15:01

+0

您的答案是好的和可能有用的琐事,但它不回答被问到的问题。 – 2016-05-08 19:47:26

+0

谢谢迈克尔,我对此很满意(即如果我的评论是OT),但不幸的是,如果你考虑可可标签+硬盘,这里的答案只是错误。我的回答隐含地包含了对已经发布的代码的更正(这应该是好的,因为这不是一种改进,而是一种修正)。所以如果你找不到剩下的正确空间,你必须使用1000的倍数而不是1024,否则结果将会不正确,也是因为可可已经在使用它。 – Mike97 2016-05-09 11:33:56

0

this post获取:

- (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这是不正确的。不与关键NSFileSystemFreeSize在它返回一个字典[:错误的NSFileManager attributesOfFileSystemForPath] -

1

通过SWIFT,您可以使用此功能

func getFreeSpace() -> CGFloat { 
     do { 
     let fileAttributes = try NSFileManager.defaultManager().attributesOfFileSystemForPath("/") 
     if let size = fileAttributes[NSFileSystemFreeSize] as? CGFloat { 
      return size 
     } 
     } catch { } 
     return 0 
    } 
+2

请勿使用相同的内容发布多个答案。你已经在这里发布了http://stackoverflow.com/a/36654477/1743880。发布一个很好的答案并将另一个标记为重复。 – Tunaki 2016-04-15 18:35:56