本文介绍了如何在MacOS上将USB硬盘和SSD与USB钥匙/笔式驱动器区分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如何一方面区分外部USB硬盘驱动器和固态驱动器,另一方面又区分USB记忆棒?

How is it possible for code to distinguish between external USB hard drives and solid-state drives on the one hand versus USB sticks on the other hand?

我不熟悉macOS API(或系统调用,中断,消息传递等),但我猜它应该在I/O Kit或磁盘仲裁中?

I'm not familiar with macOS APIs (or system calls, interrupts, messaging, and other things) but I'm guessing it would be in I/O Kit or Disk Arbitration?

在终端命令行上,您可以使用 system_profiler SPUSBDataType 并在可移动媒体"下看到此信息.

On the Terminal command line you can use system_profiler SPUSBDataType and see this information listed under "Removable Media".

推荐答案

您可以直接从URL获取可移动/可弹出信息,可弹出足以实现区分

You can get the removable/ejectable information directly from the URL, ejectable is sufficient for the differentiation

let mountedVolumeURLs = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: [.nameKey, .volumeIsEjectableKey])!
for volumeURL in mountedVolumeURLs where volumeURL.path == "/" || volumeURL.path.hasPrefix("/Volumes") {
    let resources = try! volumeURL.resourceValues(forKeys: [.nameKey, .volumeIsEjectableKey])

    let ejectable = resources.volumeIsEjectable!
    let name = resources.name!

    var type = name + " is "
    type += ejectable ? "USB stick, SD card, etc" : "hard drive, SSD, etc";
    type += " ("
    type += ejectable ? "" : "not "
    type += "ejectable)"
    print(" ", type)
}

这篇关于如何在MacOS上将USB硬盘和SSD与USB钥匙/笔式驱动器区分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:08
查看更多