简体   繁体   English

以编程方式检索 OS X 磁盘分区 UUID

[英]Programmatically retrieve an OS X disk partition UUID

I have a path to a partition.我有一个分区的路径。 How can I retrieve UUID of that partition programatically without using terminal commands?如何在不使用终端命令的情况下以编程方式检索该分区的 UUID? An example will be more helpful.一个例子会更有帮助。

$ diskutil info / | grep UUID

Running this from C is left as an exercise for the reader.从 C 运行它留给读者作为练习。

If you want a partition other than the root, you can specify the mount point or device name (eg. disk0s2) in place of / .如果您想要除根以外的分区,则可以指定挂载点或设备名称(例如 disk0s2)代替/

You can use the Disk Arbitration framework ( Apple reference ).您可以使用磁盘仲裁框架( Apple 参考)。 There is also a good summary at this blog by Chris Suter. Chris Suter 在这个博客上也有一个很好的总结。

You can get the UUID by using the kDADiskDescriptionMediaUUIDKey.您可以使用 kDADiskDescriptionMediaUUIDKey 获取 UUID。 Aaron Burghardt described it well in this mailing list thread . Aaron Burghardt 在这个邮件列表线程中很好地描述了它。 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).获得 DADisk 后,使用 DADiskCopyDescription 获取属性字典,您将在其中找到带有键 kDADiskDescriptionMediaUUIDKey 的 UUID(有关可能感兴趣的其他键,请参见 DADisk.h)。 Note, a DADisk is a wrapper around an IOMedia object and the description dictionary corresponds directly to the properties in the IOMedia object.请注意,DADisk 是 IOMedia object 的包装器,描述字典直接对应于 IOMedia object 中的属性。 Also, CFShow() is useful for printing the description dictionary to the console.此外,CFShow() 可用于将描述字典打印到控制台。

I think the easiest is to use polkit我认为最简单的是使用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.mhttp://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.h http://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.m下载 DiskWatcher.h 和.m

Add it to your project (It has no ARC so add -fno-objc-arc flag if you use ARC)将其添加到您的项目中(它没有 ARC,因此如果您使用 ARC,请添加 -fno-objc-arc 标志)

Add DiskArbiratation framework You can use添加 DiskArbiratation 框架你可以使用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM