【问题标题】:How do you find how much disk space is left in Cocoa?您如何找到 Cocoa 中剩余的磁盘空间?
【发布时间】:2010-12-10 05:08:37
【问题描述】:

我想我希望能够找到任何存储,而不仅仅是系统磁盘,但这是最重要的。

【问题讨论】:

    标签: objective-c cocoa hard-drive


    【解决方案1】:

    【讨论】:

    • -[NSFileManager attributesOfFileSystemForPath:error:] 不返回包含键 NSFileSystemFreeSize 的字典。
    • 哎呀,你是对的。看起来我在课堂上试过了,而不是实例。
    • 嗨,如果路径中的一个子文件夹指向另一个文件系统,NSFileManager 是否知道在检索属性时忽略它?
    【解决方案2】:

    我用这个:

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

    【讨论】:

    • 完美的代码!像魅力一样工作!非常感谢分享。 :)
    • 我的 2 美分 - 1) 使用包含卷路径的 NSString 代替 @"/" 来查看其他卷的信息。 2) 如果我们使用 unsignedLongLongValue 而不是 longLongValue 会更好。
    【解决方案3】:

    编辑 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;
    }
    

    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;
    }
    

    【讨论】:

    • “……使用 attributesOfFileSystemForPath:error: 代替。”这就是 NSD 的建议,你告诉他这行不通。现在可以用了吗?
    • 是的,确实如此。看来我之前搞错了。
    • 两个批评:error: 参数采用指向对象指针的指针,NSError **。因此,正确的空指针常量不是nil(它是指向对象的指针,<class-name> *id),而是NULL。更重要的是,不要抑制错误返回——将指针传递给那里的变量,并在(且仅当)尝试检索属性失败时报告错误。无声的失败是不好的。
    • 你在这两个方面都是对的。为示例代码添加了更好的处理以更具指导性。
    • fileSystemAttributesAtPath 现已弃用。
    【解决方案4】:

    硬盘设备制造商,使用:

    1GB = 1000MB

    1MB = 1000 KB 等等

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

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

    至少在 OSX 中

    【讨论】:

    • 对于那些不赞成投票的人,我邀请查看希腊词汇以了解 giga、mega 等的含义。
    • 您的回答很好,并且可能有用的琐事,但它没有回答所提出的问题。
    • 谢谢迈克尔,我对此非常满意(即,如果我的评论是 OT),但不幸的是,如果您考虑可可标签 + 硬盘驱动器,这里的答案完全是错误的。我的答案隐含地包含对已发布代码的更正(应该可以,因为不是改进而是更正)。所以如果你想找到正确的剩余空间,你必须使用 1000 而不是 1024 的倍数,否则结果将不正确,也是因为可可已经使用了它。
    【解决方案5】:

    通过swift,您可以使用此功能获得空闲空间

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

    【讨论】:

    【解决方案6】:

    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,这是不正确的。

    【讨论】:

      【解决方案7】:

      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
      

      您访问该键 (key:value) 并将其类型转换为 Int 以获得可用磁盘空间。

      【讨论】:

        猜你喜欢
        • 2010-11-06
        • 1970-01-01
        • 2011-01-13
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        相关资源
        最近更新 更多