卸载驱动器/音量而不弹出 [英] Unmounting Drive/volume without ejecting
问题描述
我想要卸载磁盘时不要EJECTING。要这样做,我尝试下面的代码
{
NSString * path;
CFStringRef * volumeName =(__ bridge CFStringRef)path;
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
CFURLRef pathRef = CFURLCreateWithString(NULL,CFSTR(/ volumes / Untitled),NULL);
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault,session,pathRef);
DADiskUnmount(disk,kDADiskUnmountOptionForce,NULL,NULL);
}
想要动态路径到卷,其中在代码中它的静态。我尝试将NSString更改为CFStringRef,然后尝试在路径(/ volumes / Untitled)的位置使用,但它仍然相同。
欢迎任何建议。 / p>
首先,强烈建议您不要使用 kDADiskUnmountOptionForce
/ p>
这是一种通过基本错误处理和内存管理卸载某个卷的方法。
- (BOOL)unmountVolumeAtURL:(NSURL *)url
BOOL returnValue = NO;
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
if(session){
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault,session,(__bridge CFURLRef)url);
if(disk){
DADiskUnmount(disk,kDADiskUnmountOptionDefault,NULL,NULL);
returnValue = YES;
CFRelease(disk);
} else {
NSLog(@无法从%@创建磁盘引用,url.path);
}
} else {
NSLog(@无法创建DiskArbritation会话);
}
if(session)CFRelease(session);
return returnValue;
}
错误处理可以通过在 DADiskUnmount
函数。
I am want to unmount a disk WITHOUT EJECTING. To do that I tried following code
{
NSString *path;
CFStringRef *volumeName=(__bridge CFStringRef)path;
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
CFURLRef pathRef = CFURLCreateWithString(NULL, CFSTR("/volumes/Untitled"), NULL);
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, pathRef);
DADiskUnmount(disk, kDADiskUnmountOptionForce, NULL, NULL);
}
This code is from this question, Thanks to @zeFree
Its working but I want dynamic path to the volume where as in the code its static. I tried changing NSString to CFStringRef and then tried to use at place of path("/volumes/Untitled") mention but its still same.
Any suggestion is welcome.
First of all, you are strongly discouraged from using kDADiskUnmountOptionForce
.
This is a method to unmount a volume at given URL with basic error handling and memory management.
- (BOOL)unmountVolumeAtURL:(NSURL *)url
BOOL returnValue = NO;
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
if (session) {
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, (__bridge CFURLRef)url);
if (disk) {
DADiskUnmount(disk, kDADiskUnmountOptionDefault, NULL , NULL);
returnValue = YES;
CFRelease(disk);
} else {
NSLog(@"Could't create disk reference from %@", url.path);
}
} else {
NSLog(@"Could't create DiskArbritation session");
}
if (session) CFRelease(session);
return returnValue;
}
The error handling could be still improved by providing a callback handler in the DADiskUnmount
function.
这篇关于卸载驱动器/音量而不弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!