问题描述
我有一个OS X应用程序作为一个.app包,可以驻留在任何
任意位置的文件系统。有没有办法以编程方式从程序中找到
当前路径。
注意
getcwd()不工作,因为打开的所有包的当前目录
是通过点击打开或使用打开命令是/。如果我从myapp.app/Contents/MacOS调用可执行文件
,目录显示正确。
这是一个可可应用程序。
任何线索?
您应该可以通过 [[NSBundle mainBundle] bundleURL];
或 [[NSBundle mainBundle] bundlePath ];
。
但是,您可能需要通过 NSBundle $
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 anyarbitrary location in the filesystem. Is there a way to find thecurrent path of the package programmatically from with.
NOTE
getcwd() doesn't work since the current directory of all packages openedis via click-open or using the open command is "/" . If i invoke the executablefrom 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应用程序包的驻留目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!