Programmatically retrieve an OS X disk partition UUID

耗尽温柔 提交于 2019-12-22 08:05:22

问题


I have a path to a partition. How can I retrieve UUID of that partition programatically without using terminal commands? An example will be more helpful.


回答1:


You can use the Disk Arbitration framework (Apple reference). There is also a good summary at this blog by Chris Suter.

You can get the UUID by using the kDADiskDescriptionMediaUUIDKey. Aaron Burghardt described it well in this mailing list thread. Here is a quote from that link:

Once you have the DADisk, use DADiskCopyDescription to get a dictionary of properties, in which you will find the UUID with the key kDADiskDescriptionMediaUUIDKey (see DADisk.h for other keys that may be of interest). Note, a DADisk is a wrapper around an IOMedia object and the description dictionary corresponds directly to the properties in the IOMedia object. Also, CFShow() is useful for printing the description dictionary to the console.




回答2:


$ diskutil info / | grep UUID

Running this from C is left as an exercise for the reader.

If you want a partition other than the root, you can specify the mount point or device name (eg. disk0s2) in place of /.




回答3:


I think the easiest is to use polkit

Download the DiskWatcher.h and .m from http://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.h http://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.m

Add it to your project (It has no ARC so add -fno-objc-arc flag if you use ARC)

Add DiskArbiratation framework You can use

+ (NSString*) diskIdentifierForPath:(NSString*)path;

NSString *UUID1 = [DiskWatcher diskIdentifierForPath:@"/Volumes/Backup900GB"];


来源:https://stackoverflow.com/questions/6567565/programmatically-retrieve-an-os-x-disk-partition-uuid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!