2013-10-18 129 views
3

如何在运行时以编程方式确定给定路径是Mac OS X上的网络路径还是本地路径?以编程方式知道给定的路径在Mac OS X上是网络路径还是本地路径

例如: 1. /卷/ ABC/XYZ(安装使用SMB) 2. ../test/pqr(应用程序共享的网络路径上,因此,在当前工作目录也是一个网络路径,因此相对路径也是网络路径)

像下面的Windows一样,代码将确定pPath是否是网络共享路径(如* 1. \ TallyDT100 \ c \ test \ file.txt 2. z:\ test \ file.txt(当z:映射到某个网络路径时的网络路径)

UNIVERSAL_NAME_INFO * universalname = NULL;    ///< for getting the universal path name of file on network share. 
    DWORD     retval;        ///< for getting the return value from WNetGetUniversalName 
    DWORD     length   = MAX_PATH_LEN;  ///< length of universal name which would be made. 


// The memory for getting the universal name information is allocated. 
universalname = (UNIVERSAL_NAME_INFO *) Calloc (MAX_PATH_LEN * sizeof (Char)); 

retval = WNetGetUniversalName (pPath, UNIVERSAL_NAME_INFO_LEVEL, universalname, &length); 

Free (universalname); 

// NO_ERROR is returned only when it's drive mapped for shared network folder. 
return (NO_ERROR == retval) ? true : false; 
+0

你有特定的网络路径?? –

+0

例如:1./Volumes/abc/xyz(使用smb挂载) –

+0

或2. ../test/pqr(应用程序位于共享网络路径上,因此当前工作目录也是网络路径,因此相对路径也是一个网络路径) –

回答

0

请检查以下如何检测是否你的机器是否在你的路径存在: -

NSArray *arr=NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES); 
NSString *str=[arr lastObject]; 
NSString *yourPath=[str stringByAppendingPathComponent:@"Volumes/abc/xyz"]; 
if([[NSFileManager defaultManager]fileExistsAtPath:yourPath]) 
{ 
    NSLog(@"This is your Local Path"); 
} 
+0

感谢您的回答。还有一个疑问,只有在我的用户(/ Users/XXX/Desktop/*)桌面上的目录才会返回true。对于系统硬盘本地的其他目录(不在桌面上),它将返回false。 –

+0

没有任何路径你会给,它相应地确定。如果你想检查所有的目录,然后使用NSDirectory类的API –

2

万一有人从谷歌搜索绊倒在此,这里是苹果公司的一些有用的信息:

There are numerous ways to test whether a volume is a network volume. The best method to use depends on the layer that you're working at:

If you're working on general application code, you should use NSURL for this. Construct a URL for any item on the volume and then call -getResourceValue:forKey:error: to get the NSURLVolumeIsLocalKey key; the returned value will be false (kCFBooleanFalse) for a network volume and true (kCFBooleanTrue) for a local volume.

If you're programming at the BSD layer, you can call statfs and test the MNT_LOCAL flag in the f_flags field that it returns. Alternatively, you can call getattrlist to request the ATTR_VOL_MOUNTFLAGS attribute, which can be more efficient than statfs under some circumstances.

https://developer.apple.com/library/mac/qa/nw09/_index.html

0

您可以使用DiskArbitration

#import <DiskArbitration/DiskArbitration.h> 

+(BOOL)isNetworkVolume: (NSURL *)volumeURL 
{ 
    BOOL result = NO; 
    int     err = 0; 
    DADiskRef   disk; 
    DASessionRef  session; 
    CFDictionaryRef  descDict; 
    session = DASessionCreate(NULL); 
    if (session == NULL) { 
     err = EINVAL; 
    } 
    if (err == 0) { 
     disk = DADiskCreateFromVolumePath(NULL,session,(__bridge CFURLRef)volumeURL); 
     if (session == NULL) { 
      err = EINVAL; 
     } 
    } 
    if (err == 0) { 
     descDict = DADiskCopyDescription(disk); 
     if (descDict == NULL) { 
      err = EINVAL; 
     } 
    } 
    if (err == 0) { 
     CFBooleanRef VolumeNetworkKey = CFDictionaryGetValue(descDict,kDADiskDescriptionVolumeNetworkKey); 
     if (VolumeNetworkKey != NULL) 
     { 
      if (CFEqual(VolumeNetworkKey, kCFBooleanTrue)) { 
       result = YES; 
      } 
     } 
    } 
    if (descDict != NULL) { 
     CFRelease(descDict); 
    } 
    if (disk != NULL) { 
     CFRelease(disk); 
    } 
    if (session != NULL) { 
     CFRelease(session); 
    } 
    return result; 
}