从文件URL确定AFP共享 [英] Determine AFP share from a file URL
问题描述
我写一个应用程序将某些文件复制到某个位置。它允许用户选择始终在AFP共享上的目标。这是用NSOpenPanel来完成的。返回的URL是: file:// localhost / Volumes / Oliver%20Legg's%20Backup /
。
I writing an application to copy some files to a certain location. It allows the user to choose the destination, which is always on an AFP share. This is done with a NSOpenPanel. The URL that gets returned is: file://localhost/Volumes/Oliver%20Legg's%20Backup/
.
我想要完成的是当应用程序启动时,如果未安装AFP共享,我希望它自动挂载它。
What I want to accomplish is when the application is started, if the AFP share isn't mounted I want it to automatically mount it. What is the best way to do this?
Get Info命令将服务器列为: afp:// Power Mac G5._afpovertcp.local / Oliver%20Legg's%20Backup
。如何以编程方式从文件URL获取此URL?我想如果我可以得到该URL,我可以使用 FSMountServerVolumeAsync 。这是最好的(最简单,最抽象的)API使用?
The Get Info command lists the server as: afp://Power Mac G5._afpovertcp.local/Oliver%20Legg's%20Backup
. How can I programatically get this URL from a file URL? I think if I could get that URL I could mount it using FSMountServerVolumeAsync. Is that the best (easiest, most abstracted) API to use?
推荐答案
为了获得这些信息,没有办法用常规的Cocoa调用。使用 FSCopyURLForVolume()
获取URL,但您需要获取已加载卷的卷引用号,以便使用它:
You need to use some lower-level File Manager routines to get this information, there's no way to do it with regular Cocoa calls. The URL is obtained using FSCopyURLForVolume()
but you need to get a volume reference number for the mounted volume in order to use it:
#import <CoreServices/CoreServices.h>
//this is the path to the mounted network volume
NSString* pathToVolume = @"/Volumes/MountedNetworkVolume/";
//get the volume reference number
FSRef pathRef;
FSPathMakeRef((UInt8*)[pathToVolume fileSystemRepresentation],&pathRef,NULL);
FSCatalogInfo catalogInfo;
OSErr osErr = FSGetCatalogInfo(
&pathRef,
kFSCatInfoVolume,
&catalogInfo,
NULL,
NULL,
NULL
) ;
FSVolumeRefNum volumeRefNum = 0;
if(osErr == noErr)
volumeRefNum = catalogInfo.volume;
//get the server URL for the volume
CFURLRef serverLocation;
OSStatus result = FSCopyURLForVolume (volumeRefNum,&serverLocation);
if(result == noErr)
NSLog(@"The server location is: %@",serverLocation);
else
NSLog(@"An error occurred: %i",result);
CFRelease(serverLocation);
FSMountServerVolumeAsync
绝对是正确的挂载方式一个远程卷。
FSMountServerVolumeAsync
is definitely the correct way to mount a remote volume.
这篇关于从文件URL确定AFP共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!