列出所有驱动器分区,并使用Cocoa获取 devrdisc设备

列出所有驱动器分区,并使用Cocoa获取 devrdisc设备,第1张

概述有没有办法列出可用的驱动器,类似于磁盘工具,并获取相关的/ dev / rdisk *设备? 磁盘工具可以访问此数据 – 当您选择驱动器并按信息按钮时,它会列出.. Partition Map Scheme : GUID Partition TableDisk Identifier : disk0Media Name : Hitachi HTS541612J9SA00 Media ..或选择一 有没有办法列出可用的驱动器,类似于磁盘工具,并获取相关的/ dev / rdisk *设备?

磁盘工具可以访问此数据 – 当您选择驱动器并按信息按钮时,它会列出..

Partition Map Scheme : GUID Partition tabledisk IDentifIEr : disk0Media name : Hitachi HTS541612J9SA00 Media

..或选择一个分区

disk IDentifIEr : disk0s3Mount Point : /Volumes/BOOTCAMP

是否有一个Cocoa API来实现这个目标?如果是这样,通过Interface Builder显示它的最佳方法是什么?

解决方法 土拨鼠指出,IORegistry确实是与设备相关的所有东西的首选来源. IOKit文档非常详细和有用;你应该从 IOKit Fundamentals开始,然后点击 Accessing Hardware from Applications,然后最终查看 Device File Access Guide for Storage Devices,如果你想获得BSD方式的信息.

在这种情况下,您可以使磁盘仲裁框架执行查询IO注册表和注册通知的繁重工作.这样可以节省大量代码,但是要完成您想要做的所有事情,最终需要将IOKit函数与基础IOMedia对象一起使用(参见DADiskCopyIOMedia()).

您可以在代表IO注册表中的磁盘的IOMedia对象周围编写Cocoa包装器,然后使用对象控制器将属性绑定到UI.

以下是通过磁盘仲裁框架注册磁盘外观通知的示例,以帮助您入门:

// gcc -Wall -framework Foundation -framework diskarbitration disk_arbiter.m -o disk_arbiter/* @file disk_arbiter.m * @author Jeremy W. Sherman * @date 2009-10-03 * * Demonstrates registering for disk appeared notifications from * the diskarbitration framework. * * Note that disk appeared notifications are delivered for all * already-appeared disks at the time of registration,and then * trickle in as the events actually happen thereafter. */#import <Foundation/Foundation.h>#import <diskarbitration/diskarbitration.h>#import <signal.h>sig_atomic_t sShouldExit = 0;static voID RegisterInterruptHandler(voID);static voID HandleInterrupt(int);static voID OndiskAppeared(DAdiskRef disk,voID *__attribute__((__unused__)));intmain(voID) {  CFStringRef const kDARunLoopMode = kcfRunLoopDefaultMode;  RegisterInterruptHandler();  // Set up session.  DASessionRef session = DASessionCreate(kcfAllocatorDefault);  DARegisterdiskAppearedCallback(session,NulL/*all disks*/,OndiskAppeared,(voID *)NulL);  DASessionScheduleWithRunLoop(session,CFRunLoopGetCurrent(),kDARunLoopMode);  // Run event loop.  printf("Starting...\n(Press Ctrl-C to exit.)\n\n");  const Boolean kAndReturnAfterHandlingSource = TRUE;  const CFTimeInterval kForOnesecond = 1.0;  while (!sShouldExit)    (voID)CFRunLoopRunInMode(kcfRunLoopDefaultMode,kForOnesecond,kAndReturnAfterHandlingSource);  // Tear down and exit.  printf("\nExiting...\n");  DASessionUnscheduleFromrunLoop(session,kDARunLoopMode);  CFRelease(session);  exit(EXIT_SUCCESS);  return EXIT_SUCCESS;}static voIDRegisterInterruptHandler(voID) {  struct sigaction sigact;  sigact.sa_handler = HandleInterrupt;  (voID)sigaction(SIGINT,&sigact,NulL/*discard prevIoUs handler*/);}static voIDHandleInterrupt(int __attribute__((__unused__)) signo) {  sShouldExit = 1;  RegisterInterruptHandler();}static voIDOndiskAppeared(DAdiskRef disk,voID *__attribute__((__unused__)) ctx) {  printf("Lo,a disk appears!\n");  CFShow(disk);}

这是样本运行的输出:

$./disk_arbiter Starting...(Press Ctrl-C to exit.)Lo,a disk appears!<DAdisk 0x104f80 [0xa01c01a0]>{ID = /dev/disk3}Lo,a disk appears!<DAdisk 0x105b40 [0xa01c01a0]>{ID = /dev/disk2s1}Lo,a disk appears!<DAdisk 0x105ae0 [0xa01c01a0]>{ID = /dev/disk2s2}Lo,a disk appears!<DAdisk 0x105b60 [0xa01c01a0]>{ID = /dev/disk2}Lo,a disk appears!<DAdisk 0x105950 [0xa01c01a0]>{ID = /dev/disk1}Lo,a disk appears!<DAdisk 0x105bc0 [0xa01c01a0]>{ID = /dev/disk1s1}Lo,a disk appears!<DAdisk 0x105540 [0xa01c01a0]>{ID = /dev/disk0}Lo,a disk appears!<DAdisk 0x105660 [0xa01c01a0]>{ID = /dev/disk0s1}Lo,a disk appears!<DAdisk 0x1054a0 [0xa01c01a0]>{ID = /dev/disk0s2}^CExiting...
总结

以上是内存溢出为你收集整理的列出所有驱动器/分区,并使用Cocoa获取/ dev / rdisc设备全部内容,希望文章能够帮你解决列出所有驱动器/分区,并使用Cocoa获取/ dev / rdisc设备所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1019399.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存