os x的cocoa objective-c:从路径获取卷装入点

Mic*_*l C 6 cocoa objective-c

我想找到给定NSString路径的卷的挂载点.

虽然我是Cocoa和Objective-C的初学者,但我试图"优雅地"这样做,即.使用提供的类之一,而不是进行外部shell调用或列出已安装的文件系统并找到路径所属的类.

我确实找到了NSWorkspace和getFileSystemInfoForPath,但它没有提到挂载点.

有人可以帮忙吗?

谢谢

Pie*_*ard 4

这应该遵循以下原则:

+ (NSString*)volumeNameForPath:(NSString *)inPath
{
    HFSUniStr255 volumeName;
    FSRef volumeFSRef;
    unsigned int volumeIndex = 1;

    while (FSGetVolumeInfo(kFSInvalidVolumeRefNum, volumeIndex++, NULL, kFSVolInfoNone, NULL, &volumeName, &volumeFSRef) == noErr) {
        NSURL *url = [(NSURL *)CFURLCreateFromFSRef(NULL, &volumeFSRef) autorelease];
        NSString *path = [url path];

        if ([inPath hasPrefix:path]) {
            return [NSString stringWithCharacters:volumeName.unicode length:volumeName.length]
        }
    }

    return nil;
}
Run Code Online (Sandbox Code Playgroud)