如何枚举Mac OS X上的卷? [英] How to enumerate volumes on Mac OS X?

查看:162
本文介绍了如何枚举Mac OS X上的卷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am not very proficient in Mac OS X programming, but I am working on a Qt application which needs info about the storage devices. Basically a list of hard drives and USB thumb drives. The end result should be like a vector which contains the following info for each device:

string:标签

string:Mount点

string:设备描述(又名友好名称)

uint64:大小

bool:可移动?

string: Label
string: Mount point
string: Device description (aka friendly name)
uint64: Size
bool: Is removable?

我一直在Windows和下面的帖子 http://stackoverflow.com/questions/3098696/how-to-get-the-vid-pid-and -drive-letter-for-all-the-usb-mass-storage-devices-plug 已经有很大的帮助。然而,虽然我非常精通C / C + +我不是真的好在Mac OS X编程,Cocoa和/或Objective-C,所以任何帮助将非常感激。

I've been doing it on Windows and the following post http://stackoverflow.com/questions/3098696/how-to-get-the-vid-pid-and-drive-letter-for-all-the-usb-mass-storage-devices-plug have been of a great help. However, although I am very proficient in C/C++ I am not really good in Mac OS X programming, Cocoa and/or Objective-C, so any help would be much appreciated.

推荐答案

这应该能帮你找到你想要的东西:

This should get you most of what you're looking for:

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

这篇关于如何枚举Mac OS X上的卷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆