3

NSWorkspaceDidMountNotification works well to get the information of just mounted disk. But how can I get the information of already mounted disks before my app start?

command line: "diskutil list" and "diskutil info /" works but there should be a simple programmatically method there.

searched result of "DiskArbitration" or "VolumeToBSDNode example" don't work, IOkit difficult.

BTW, anyone recommend of using this? [NSWorkspace getFileSystemInfoForPath:isRemovable:isWritable:isUnmountable:description:type:]

Jiulong Zhao
  • 1,313
  • 1
  • 15
  • 25

1 Answers1

7

How about [NSFileManager mountedVolumeURLsIncludingResourceValuesForKeys:options:]?

Edit: Here's a snippet of code for how to use this to get removable drives and their volume names.

NSArray *keys = [NSArray arrayWithObjects:NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, nil];
NSArray *urls = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];
for (NSURL *url in urls) {
  NSError *error;
  NSNumber *isRemovable;
  NSString *volumeName;
  [url getResourceValue:&isRemovable forKey:NSURLVolumeIsRemovableKey error:&error];
  if ([isRemovable boolValue]) {
    [url getResourceValue:&volumeName forKey:NSURLVolumeNameKey error:&error];
    NSLog(@"%@", volumeName);
  }
}
Yunchi
  • 5,529
  • 2
  • 17
  • 18
  • Hi Woody, grate!!!! it's almost there with this:NSArray *result = [testM mountedVolumeURLsIncludingResourceValuesForKeys:nil options:NSVolumeEnumerationSkipHiddenVolumes]; but can not find out which is the mounted removable, which is the local. And how to convert them into disk name like "HP v125w"? "file://localhost/Volumes/HP%20v125w/" – Jiulong Zhao Aug 09 '12 at 22:34
  • tried this with no luck: NSArray *proA = [NSArray arrayWithObjects:NSURLVolumeIdentifierKey,NSURLTypeIdentifierKey,nil]; NSArray *result = [testM mountedVolumeURLsIncludingResourceValuesForKeys:proA options:NSVolumeEnumerationSkipHiddenVolumes]; – Jiulong Zhao Aug 09 '12 at 22:48
  • Don't have access to xCode at the moment, but could you try [NSURLVolumeIsRemovableKey](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLVolumeIsRemovableKey)? – Yunchi Aug 09 '12 at 23:26
  • Yes, the code works! the 150 credit goes to you. One more confirmation: should we use options:NSVolumeEnumerationSkipHiddenVolumes instead of options:0 ? – Jiulong Zhao Aug 10 '12 at 05:45
  • @JiulongZhao Thanks for the rep. As far as I can see, hidden volumes tend not to be mounted, and it takes quite a bit of effort to mount them. An example would be the Recovery HD partition. It's should be safe to skip over them unless you have some reason not to. – Yunchi Aug 10 '12 at 12:16