我正在嘗試在可用磁盤上顯示資料。我想顯示的一件事是它的檔案系統型別。我應該在這段代碼中添加什么:\
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("")
print(url.name ?? "Untitled")
print("Capacity:", url.volumeTotalCapacity ?? "nil")
print("Available:", url.volumeAvailableCapacityForImportantUsage ?? "nil")
print("File System type:", url.isFileURL)
//print("File System Type:", )
}
uj5u.com熱心網友回復:
您應該使用 URLResourceKeyvolumeLocalizedFormatDescriptionKey來獲取磁盤檔案系統
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,格式:APFS
Bilder-Arkiv,格式:Mac OS Extended (Journaled)
LEICA M9,格式:MS-DOS (FAT32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/386739.html
上一篇:創建和檢索JSON格式的資料
