【问题标题】:How to enumerate volumes on Mac OS X?如何在 Mac OS X 上枚举卷?
【发布时间】:2011-04-02 11:40:28
【问题描述】:

我对 Mac OS X 编程不是很精通,但我正在开发一个需要有关存储设备信息的 Qt 应用程序。基本上是硬盘驱动器和 USB 拇指驱动器的列表。 最终结果应该像一个向量,其中包含每个设备的以下信息:

字符串:标签
字符串:挂载点
字符串:设备描述(又名友好名称)
uint64:大小
bool: 是可移动的吗?

我一直在 Windows 上进行此操作,以下帖子 Get information about disk drives result on windows7 - 32 bit system 提供了很大帮助。然而,虽然我非常精通 C/C++,但我在 Mac OS X 编程、Cocoa 和/或 Objective-C 方面并不是很擅长,因此非常感谢任何帮助。

【问题讨论】:

    标签: c++ objective-c macos qt


    【解决方案1】:

    嗯,回到我们使用FSGetVolumeInfo 的那一天。至于可移动性,那将是 FSGetVolumeParms 使用 vMExtendedAttributes & 1<< bIsRemovable。 (实际上,我不记得那个特定的 API。有一个叫做 Driver Gestalt 的东西,但现在已经不存在了。)

    我想有一个闪亮的 Objective-C 接口,但如果没有其他人回复,至少有 C 方式。

    【讨论】:

    • FSGetVolumeInfoFSGetVolumeParms 的链接现在已断开。
    【解决方案2】:

    查看getmntinfo()(关于挂载点的枚举)和statfs()(关于已知挂载点的信息。)

    【讨论】:

      【解决方案3】:

      这应该可以为您提供大部分所需的内容:

      NSWorkspace   *ws = [NSWorkspace sharedWorkspace];
      NSArray     *vols = [ws mountedLocalVolumePaths];
      NSFileManager *fm = [NSFileManager defaultManager];
      
      for (NSString *path in vols) 
      {
          NSDictionary* fsAttributes;
          NSString *description, *type, *name;
          BOOL removable, writable, unmountable, res;
          NSNumber *size;
      
          res = [ws getFileSystemInfoForPath:path 
                                 isRemovable:&removable 
                                  isWritable:&writable 
                               isUnmountable:&unmountable
                                 description:&description
                                        type:&type];
          if (!res) continue;
          fsAttributes = [fm fileSystemAttributesAtPath:path];
          name         = [fm displayNameAtPath:path];
          size         = [fsAttributes objectForKey:NSFileSystemSize];
      
          NSLog(@"path=%@\nname=%@\nremovable=%d\nwritable=%d\nunmountable=%d\n"
                 "description=%@\ntype=%@, size=%@\n\n",
                path, name, removable, writable, unmountable, description, type, size);
      }
      

      【讨论】:

      • 谢谢。这正是我所需要的:) 我没有找到一种方法来显示像 Windows 上的“友好名称”这样的信息,尽管我想有一种方法可以在 Mac 上做到这一点(我认为这是这里的描述字段,但它是一个文件系统)。如果您知道如何检索该信息,请告诉我...否则,这太棒了:)
      • @emi:嗯,我不知道。我什至无法使用 I/O Registry Explorer(位于 /Developer/Applications/Utilities)找到类似的东西。
      • 这曾经是正确的答案,但现在已弃用。有关当前答案,请参阅此问题:stackoverflow.com/questions/10852056/…
      猜你喜欢
      • 2011-03-27
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 2014-11-29
      • 2011-04-10
      • 2012-07-27
      • 2011-04-20
      相关资源
      最近更新 更多