2013-10-18 174 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; 
}