【问题标题】:What is the path to the Mac OSX application Contents folderMac OSX 应用程序 Contents 文件夹的路径是什么
【发布时间】:2013-05-20 19:37:27
【问题描述】:

我正在编写一个 Objective-c++ 应用程序,我正在尝试访问应用程序 Contents 文件夹。

假设我的应用名称是 MyApp.app。如果我从 Xcode 运行我的应用程序,那么我可以像这样访问Contents 文件夹:

std::fstream file;
file.open("MyApp.app/Contents/some_text_file.txt");
if(file.is_open()) {
...
}
else {
...
}

但是如果我在没有 Xcode 的情况下运行我构建的应用程序,那么 file.is_open() 会失败。

【问题讨论】:

    标签: objective-c++


    【解决方案1】:

    [[NSBundle mainBundle] bundlePath] 将为您提供MyApp.app 目录的路径。然后,您可以将Contents 添加到该路径。

    或者,[[NSBundle mainBundle] resourcePath] 将为您提供MyApp.app/Contents/Resources 目录的路径。然后,您可以从该路径中删除子目录;见Working with paths from [[NSBundle mainBundle] resourcePath]

    【讨论】:

    • 感谢您的详细解答!
    【解决方案2】:

    使用NSBundle 类获取有关您的应用程序包的信息。 [[NSBundle mainBundle] bundlePath] 为您提供 MyApp.app 的完整路径。

    【讨论】:

      猜你喜欢
      • 2011-03-27
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      相关资源
      最近更新 更多