代码之家  ›  专栏  ›  技术社区  ›  itsaboutcode

在目标C中获取当前目录

  •  20
  • itsaboutcode  · 技术社区  · 16 年前

    我试图得到当前目录,但它给了我调试文件夹的路径,我如何才能得到当前目录的路径。我正在使用以下代码。

    NSFileManager *filemgr;
    NSString *currentpath;
    
    filemgr = [[NSFileManager alloc] init];
    
    currentpath = [filemgr currentDirectoryPath];
    
    1 回复  |  直到 16 年前
        1
  •  27
  •   mipadi    16 年前

    currentDirectoryPath 返回程序的当前工作目录。如果在调试模式下从Xcode启动它,程序的当前目录 调试目录。

        2
  •  0
  •   lupdidup    6 年前

    ~ ).

    Library/Developer/Xcode/DerivedData/MyApp-ehwczslxjxsxiob/Build/Products/Debug/MyApp.app/Contents/MacOS/MyApp -param value
    
    NSString *p1 = [[NSFileManager defaultManager] currentDirectoryPath];
    // p1 = ~/Library/Containers/com.MyApp.beta/Data
    NSString *p2 = [[NSProcessInfo processInfo] environment][@"PWD"];
    // p2 = ~
    

    视情况而定,你可能想要一个或另一个。在我的例子中,我想提供一个CLI,它接受可能与shell中当前工作目录相关的路径参数。

    推荐文章