如何使用 Swift 显示磁盘文件系统类型(HFS+、APFS 等)

How can I display Disk File System Type (HFS+, APFS etc) using Swift

我正在尝试显示可用磁盘上的数据。我要显示的其中一件事是它的文件系统类型。我应该在这段代码中添加什么:\

extension FileManager {
    static var mountedVolumes: [URL] {
        (FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil) ?? []).filter({[=11=].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("")
    print(url.name ?? "Untitled")
    print("Capacity:", url.volumeTotalCapacity ?? "nil")
    print("Available:", url.volumeAvailableCapacityForImportantUsage ?? "nil")
    print("File System type:", url.isFileURL)
    //print("File System Type:", )
}

您应该使用 URLResourceKey volumeLocalizedFormatDescriptionKey 来获取磁盘文件系统

let fileManager = FileManager.default
let resourceKeys: [URLResourceKey] = [.volumeNameKey, .volumeLocalizedFormatDescriptionKey]

if let volumes = fileManager.mountedVolumeURLs(includingResourceValuesForKeys: resourceKeys, options: .skipHiddenVolumes) {
    for volume in volumes {
        if let resources = try? volume.resourceValues(forKeys: Set(resourceKeys)), 
           let name = resources.volumeName, 
           let format = resources.volumeLocalizedFormatDescription {
            print("\(name), format: \(format)")
        }
    }
}

示例来自我的机器,带有外部驱动器和 SD 卡

Macintosh HD, format: APFS
Bilder-Arkiv, format: Mac OS Extended (Journaled)
LEICA M9, format: MS-DOS (FAT32)