检测 osx 上卷的出现/消失
我想在用户插入 USB 密钥、添加外部磁盘和安装磁盘映像时更新存储设备列表。 IOKit 的 IOServiceAddInterestNotification 看起来像是可行的方法,但在 kIOMediaClass 中注册一般兴趣的明显用途只是为您提供卸载卷的通知,而且只是有时而已。
这样做的正确方法是什么?
I want to update a list of storage devices as the user inserts USB keys, adds external disks and mounts disk images. IOKit's IOServiceAddInterestNotification looks like the way to go, but the obvious use of registering general interest in kIOMediaClass only gives you notifications for unmounting of volumes and then only sometimes.
What's the right way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
DiskArbitration.h 中的以下调用完全符合我的要求:
DARegisterDiskAppearedCallback
DARegisterDiskDisappearedCallback
DARegisterDiskDescriptionChangedCallback
这些涵盖插入、删除(甚至是不可安装的卷)
元数据更改事件。
PS 不要忘记将您的
DASession
添加到运行循环否则您将不会收到任何回调。
The following calls in DiskArbitration.h do exactly what I want:
DARegisterDiskAppearedCallback
DARegisterDiskDisappearedCallback
DARegisterDiskDescriptionChangedCallback
These cover insertion, removal (even of unmountable volumes)
metadata change events.
P.S. Don't forget to add your
DASession
to a runloopor you won't get any callbacks.
我可以用这段代码为您提供三分之二,我想不需要做更多的工作即可为您提供第三个。
http://opensource .apple.com/source/IOUSBFamily/IOUSBFamily-385.4.1/Examples/Another%20USB%20Notification%20Example/USBNotificationExample.c
我个人使用了很长时间(此代码的稍微修改的副本) ,监控 USB HDD 的连接。
正如您从这个小示例中看到的,它可以轻松地证明适用于监视已安装的驱动器。 或者也可能不会。 YMMV。
以及何时匹配
等等。
I can get you two out of three with this piece of code, which I imagine wouldn't require a lot more work to give you the third.
http://opensource.apple.com/source/IOUSBFamily/IOUSBFamily-385.4.1/Examples/Another%20USB%20Notification%20Example/USBNotificationExample.c
I've personally used (a slightly modified copy of this code) for a long time, to monitor the connection of USB HDDs.
As you can see from this small sample, it may easily prove adaptable to monitor mounted drives. Or it may not. YMMV.
and when it matches
and so forth, and so on.
观察
/Volumes
的变化是否能满足您的需要?Would watching
/Volumes
for changes do what you need?如果您恰好在 Cocoa 级别工作,您还可以注册以接收来自 NSWorkspace:
If you happen to be working at the Cocoa level, you can also register to receive the following notifications from NSWorkspace: