获取Swift的装载卷列表?

new9mtju  于 2022-12-21  发布在  Swift
关注(0)|答案(3)|浏览(138)

有人知道如何获得所有可移动卷的列表安装与斯威夫特?
我已经试过了,但它返回外部驱动程序的所有文件和子文件夹的列表:

let filemanager:NSFileManager = NSFileManager()
let files = filemanager.enumeratorAtPath("/Volumes")

while let file = files?.nextObject() {
    println(file)
    menu.addItem(NSMenuItem(title: file as! String, action: Selector(""), keyEquivalent: ""))
}
rseugnpd

rseugnpd1#

这将打印所有已装载卷的列表:

let filemanager = NSFileManager()
let keys = [NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, NSURLVolumeIsEjectableKey]
let paths = filemanager.mountedVolumeURLsIncludingResourceValuesForKeys(keys, options: nil)
if let urls = paths as? [NSURL] {
    for url in urls {
        println(url)
    }
}

当然,您可以进行过滤,仅获取“Volumes”目录中的路径:

let filemanager = NSFileManager()
let keys = [NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, NSURLVolumeIsEjectableKey]
let paths = filemanager.mountedVolumeURLsIncludingResourceValuesForKeys(keys, options: nil)
if let urls = paths as? [NSURL] {
    for url in urls {
        if url.relativePath?.pathComponents.count > 1 {
            if url.relativePath?.pathComponents[1] == "Volumes" {
                println(url)
            }
        }
    }
}

Swift 2有两个不同之处:为文件管理器的选项传递[]而不是nil,并且不需要强制转换NSURL数组:

let filemanager = NSFileManager()
let keys = [NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, NSURLVolumeIsEjectableKey]
let paths = filemanager.mountedVolumeURLsIncludingResourceValuesForKeys(keys, options: [])
if let urls = paths {
    for url in urls {
        if url.relativePath?.pathComponents.count > 1 {
            if url.relativePath?.pathComponents[1] == "Volumes" {
                print(url)
            }
        }
    }
}

针对Swift 2.1的更新

let keys = [NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, NSURLVolumeIsEjectableKey]
let paths = NSFileManager().mountedVolumeURLsIncludingResourceValuesForKeys(keys, options: [])
if let urls = paths {
    for url in urls {
        if let components = url.pathComponents
            where components.count > 1
            && components[1] == "Volumes" {
                print(url)
        }
    }
}

Swift 3的更新

let keys: [URLResourceKey] = [.volumeNameKey, .volumeIsRemovableKey, .volumeIsEjectableKey]
let paths = FileManager().mountedVolumeURLs(includingResourceValuesForKeys: keys, options: [])
if let urls = paths {
    for url in urls {
        let components = url.pathComponents
        if components.count > 1
           && components[1] == "Volumes"
        {
            print(url)
        }
    }
}
q5lcpyga

q5lcpyga2#

在Unix系统上,系统文件号为2的文件系统对象是挂载,无论是远程挂载(nfs、smb、afp)还是本地挂载。
下面是一个例子:

let path = "/System/Volumes/Preboot"
let systemAttributes = try FileManager.default.attributesOfItem(atPath: String(describing: path))
if let fileSystemFileNumber = systemAttributes[.systemFileNumber] as? NSNumber {
    print("System File Number: \(fileSystemFileNumber)")
}

也许这是找到坐骑的捷径

fquxozlt

fquxozlt3#

let keys: [URLResourceKey] = [.volumeNameKey, .volumeIsRemovableKey, .volumeIsEjectableKey, .volumeAvailableCapacityKey, .volumeTotalCapacityKey, .volumeUUIDStringKey, .volumeIsBrowsableKey, .volumeIsLocalKey, .isVolumeKey]
            let manager = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: keys)
            if let urls = manager {
               print(urls)
            }

这段代码在MacOS上运行良好,但是在iOS上总是返回nil。有什么已知的解决方法吗?谢谢!

相关问题