2010-08-23 103 views
14

我不是很擅长Mac OS X编程,但我正在研究需要有关存储设备信息的Qt应用程序。基本上是一个硬盘驱动器和USB拇指驱动器的列表。 最终的结果应该是这样的,其包含用于每个设备的以下信息的载体:如何枚举Mac OS X上的卷?

串:标签
字符串:挂载点
字符串:设备描述(又名友好名称)
UINT64:尺寸
布尔:可移动?

我一直在Windows上这样做,以下帖子Get information about disk drives result on windows7 - 32 bit system已经有很大的帮助。然而,虽然我非常精通C/C++,但在Mac OS X编程,Cocoa和/或Objective-C中并不是很出色,所以任何帮助都将非常值得赞赏。

回答

14

这应该让你的大部分东西你正在寻找(约挂载点的信息。):

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); 
} 
+0

+1对于更多的OOP比我的答案。 :) – 2010-08-23 07:59:07

+0

谢谢。这正是我需要:) 我没有找到一种方法来显示在Windows上的'友好名称'的信息,但我想有一种方法可以在Mac上执行它(我认为这是这里的描述字段,但它是一个文件系统)。 如果你知道如何检索该信息,请告诉我...否则,这是真棒:) – Amy 2010-08-24 15:24:25

+0

@emi:嗯,我不知道。我甚至找不到使用I/O Registry Explorer(位于'/ Developer/Applications/Utilities')的东西。 – 2010-08-24 16:57:38

4

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

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

4

看看getmntinfo()(用于安装点的枚举)和statfs()