macos - 以编程方式了解给定路径是 Mac OS X 上的网络路径还是本地路径

标签 macos cocoa filepath core-foundation

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

例如: 1./Volumes/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;

最佳答案

如果有人通过 Google 搜索偶然发现这一点,以下是 Apple 提供的一些有用信息:

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

关于macos - 以编程方式了解给定路径是 Mac OS X 上的网络路径还是本地路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19449511/

相关文章:

macos - isDirectory 参数是否为 +[NSURL fileURLWithPath :isDirectory:] need to be correct?

c# - 如何使文件路径到达解决方案特定文件夹中的文件?

javascript - 使用 v-for 时的 Vue.js Assets 文件路径

objective-c - 快速替代 drawInRect

macos - rsync:大文件的性能问题

objective-c - 获取向量中的所有点(两点之间)

c# - 如何在 WPF 应用程序中引用根目录?

macos - 在 shell 脚本中使用 gimp color-to-alpha 脚本

swift - 实时更新 NSSlider 的值

objective-c - 如何获取 NSObject 的引用计数?