How to enumerate volumes on Mac OS X?

后端 未结 3 915
無奈伤痛
無奈伤痛 2020-12-24 04:27

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 th

3条回答
  •  离开以前
    2020-12-24 05:01

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

提交回复
热议问题