问题描述
您如何确定 Swift (MacOS) 中 USB 驱动器的总空间、可用空间和已用空间?
有几篇关于此的好帖子(例如:How to get the Total Disk Space and Free Disk space using AttributesOfFileSystemForpaths in swift 2.0 和 https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity),但它们都只获取操作系统驱动器的空间,而不是 USB 驱动器的空间。
例如,我有一个卷名为“myusb”的 64GB USB 驱动器,因此 MacOS 将驱动器安装在 /Volumes/myusb。 Finder 显示 U 盘的总空间为 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
}
解决方法
您可以使用 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