【问题标题】:How to get Total/Available/Used space of a USB drive in MacOS - Swift?如何在 MacOS - Swift 中获取 USB 驱动器的总/可用/已用空间?
【发布时间】:2021-06-20 16:26:47
【问题描述】:

在 Swift (MacOS) 中如何确定 USB 驱动器的总空间、可用空间和已用空间?

有几篇关于此的好帖子(例如:How to get the Total Disk Space and Free Disk space using AttributesOfFileSystemForpaths in swift 2.0https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity),但它们都只是获取操作系统驱动器的空间,而不是 USB 驱动器的空间。

例如,我有一个卷名为“myusb”的 64GB USB 驱动器,因此 MacOS 将驱动器安装在 /Volumes/myusb。 Finder 显示 USB 驱动器的总空间为 62.91GB,可用为 62.29GB,使用为 625,999,872 字节。

问题似乎在于,当我给出 USB 驱动器的路径时,由于它显然是主 / 路径的一部分,它返回的 / 是我的操作系统驱动器的信息,而不是 USB 驱动器。

这是我在尝试确定 USB 驱动器的可用空间时所做的,它返回 292298430687 字节的值(这是我的操作系统驱动器的可用空间,而不是 USB 驱动器):

/**
   Returns URL of root of USB drive - i.e. /Volumes/myusb
   Uses bundleURL as .app file being executed is located on the USB drive
*/
static func getRootURL() -> URL {
    let bundlePath = Bundle.main.bundleURL
    let bundlePathComponents = bundlePath.pathComponents
        
    let destinationRootPathURL = URL(fileURLWithPath: bundlePathComponents[0])
        .appendingPathComponent(bundlePathComponents[1])
        .appendingPathComponent(bundlePathComponents[2])
        
    return destinationRootPathURL
}

/**
   returns free space of USB drive
*/
func getAvailableSpaceInBytes() -> Int64 {
    if #available(OSX 10.13, *) {
        if let freeSpace = try? getRootURL().resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey])
                .volumeAvailableCapacityForImportantUsage {
                return freeSpace
        }
    } else {
        // Fallback on earlier versions
        guard let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: getRootURL().path),
              let freeSize = systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber
        else {
            // something failed so return nil
            return 0
        }
            
        return freeSize.int64Value
    }
        
        return 0
    }

【问题讨论】:

    标签: swift macos usb storage file-manager


    【解决方案1】:

    您可以使用 FileManager 的 mountedVolumeURLs 方法获取所有已挂载的卷并从中获取 volumeAvailableCapacityForImportantUsage 资源键/值:


    extension FileManager {
        static var mountedVolumes: [URL] {
            (FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil) ?? []).filter({$0.path.hasPrefix("/Volumes/")})
        }
    }
    

    extension URL {
        var volumeTotalCapacity: Int? {
            (try? resourceValues(forKeys: [.volumeTotalCapacityKey]))?.volumeTotalCapacity
        }
        var volumeAvailableCapacityForImportantUsage: Int64? {
            (try? resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey]))?.volumeAvailableCapacityForImportantUsage
        }
        var name: String? {
            (try? resourceValues(forKeys: [.nameKey]))?.name
        }
        
    }
    

    用法:

    for url in FileManager.mountedVolumes {
        print(url.name ?? "Untitled")
        print("Capacity:", url.volumeTotalCapacity ?? "nil")
        print("Available:", url.volumeAvailableCapacityForImportantUsage ?? "nil")
        print("Used:", (try? url.sizeOnDisk()) ?? "nil") // check the other link below
    }
    

    对于带有 BigSur 安装程序的 16GB USB 驱动器,将打印上面的代码

    安装 macOS Big Sur
    容量:15180193792
    可用:2232998976
    已使用:磁盘上 12.93 GB


    要获取卷“sizeOnDisk”的已用空间,您可以查看post

    【讨论】:

    • 这非常有效。感谢您详细而明确的回答
    猜你喜欢
    • 2013-06-19
    • 2012-05-24
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多