如何以编程方式查找OS X应用程序包的驻留目录 [英] How to find the residing directory of a OS X application package - programmatically

查看:91
本文介绍了如何以编程方式查找OS X应用程序包的驻留目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OS X应用程序作为一个.app包,可以驻留在任何
任意位置的文件系统。有没有办法以编程方式从程序中找到
当前路径。



注意



getcwd()不工作,因为打开的所有包的当前目录
是通过点击打开或使用打开命令是/。如果我从myapp.app/Contents/MacOS调用可执行文件
,目录显示正确。


这是一个可可应用程序。



任何线索?

解决方案

您应该可以通过 [[NSBundle mainBundle] bundleURL]; [[NSBundle mainBundle] bundlePath ];



但是,您可能需要通过 NSBundle 如果您打算修改本地化问题,应用程序包; 不要。 您不应该,不能,也不能假设您对应用程序包有写访问权限;

编辑:作为一个兴趣点,如果你是在操作系统上编写一个C程序X并需要找到可执行文件的位置;你可以使用一个非可移植的主要声明如下:

  int main(int argc,const char * argv [],const char * env [],const char * path []){
// path [0]现在包含可执行文件的路径
// NOT PORTABLE! OS X ONLY!
return 0;
}


I have a OS X application as a .app package which can reside in any arbitrary location in the filesystem. Is there a way to find the current path of the package programmatically from with.

NOTE

getcwd() doesn't work since the current directory of all packages opened is via click-open or using the open command is "/" . If i invoke the executable from myapp.app/Contents/MacOS , the directory is shown correctly. It is a cocoa application.

Any clues?

解决方案

You should be able to find the location of the .app by [[NSBundle mainBundle] bundleURL]; or [[NSBundle mainBundle] bundlePath];.

However, you probably want to access resources inside the .app by going through NSBundle to locate them in most cases (this deals with issues like localization).

Note: If you are planning to modify the application bundle; don't. You should not, can not and must not assume that you have write access to the application bundle; and if you do, something is likely very wrong.

Edit: As a point of interest, if you are writing a C program on OS X and need to find the location of the executable; you can use a non-portable main declaration like so:

int main (int argc, const char *argv[], const char *env[], const char *path[]) {
  // path[0] now contains the path to the executable
  // NOT PORTABLE! OS X ONLY!
  return 0;
}

这篇关于如何以编程方式查找OS X应用程序包的驻留目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆