Cocoa Unmounting drive but not ejecting it

和自甴很熟 提交于 2019-12-12 18:04:13

问题


Do you know you to unmount a drive without ejecting it. NSWorkspace has some methods to unmount drives but it also eject them.

Any idea ?


回答1:


I am doing it as follows and it un-mounts the drive but doesn't eject it.

(Actually I want to eject the disk, I can only un-mount the disk. :P Please share how to eject a disk.)

DASessionRef session = DASessionCreate(kCFAllocatorDefault);

CFURLRef path = CFURLCreateWithString(NULL, CFSTR("<path_to_your_volume_here>"), NULL);
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, path);

DADiskUnmount(disk, kDADiskUnmountOptionDefault, __unmountCallback, NULL);

This is the code I am still working on and is under development and testing.
I am creating the "path" manually. You can use (and share) a better method to get the path of volume in a generic way. Perhaps this answer has hints of doing it the right way.

I'll update when my development is refined and complete.




回答2:


To do this, use DADiskUnmount in DiskArbitration framework.

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/DiscArbitrationFramework/DiskArbitration_h/index.html#//apple_ref/c/func/DADiskUnmount




回答3:


To eject the disk, unmount the disk as you stated, and then in your __unmountCallback do the following:

            DADiskRef disk2 = DADiskCopyWholeDisk(disk);
            DADiskEject(disk2,
                        kDADiskEjectOptionDefault,
                        NULL,
                        NULL);

You can pass any object as context to the DADiskUnmount() and then, for example, use it to determine if the respective disk should be ejected in the __unmountCallback.



来源:https://stackoverflow.com/questions/11513958/cocoa-unmounting-drive-but-not-ejecting-it

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