2010-08-23 100 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()