查找“~/Library/Application Support”来自 C++?
我已经编写了一个 GTKmm 应用程序,并且正在尝试创建一些 OS X 增强功能。我想将配置文件存储在 Application Support/myApp 文件夹中,但是,我无法找到找到该文件夹的正确方法。
我尝试查看 Core Foundation 库(我用它来获取 myApp.app 路径),但我找不到任何东西。
I've written a GTKmm application and I'm trying to create some OS X enhancements. I'd like to store my configuration file in the Application Support/myApp folder, however, I can't figure out the proper way to locate this folder.
I've tried looking through the Core Foundation library (that I'm using to get my myApp.app path) but I can't find anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C/C++ 中执行此操作的正确方法:
当然,这些都是非常古老的 API,Apple 不再推荐使用它们。尽管如此,如果你想在你的代码库中完全避免 Objective-C,它就可以完成工作。
Proper way to do it in C/C++:
Naturally, those are very old APIs and their use is no longer recommended by Apple. Despite that it gets the job done if you want to avoid Objective-C completely in your codebase.
看来用于此目的的函数是 NSSearchPathForDirectoriesInDomains(或同一页面上列出的其他一些函数),以
NSApplicationSupportDirectory
作为参数。It appears that the function to use for this is NSSearchPathForDirectoriesInDomains (or some other functions listed on the same page) with
NSApplicationSupportDirectory
as the argument.在 BSD Unix(包含在 OS-X 中)中,您可以使用以下命令获取运行程序的用户的主目录:
使用此命令,您可以使用此命令代替 ~ 构建路径
In BSD Unix, included in OS-X, you can get the home directory of the user running the program with this:
Using this, you can then construct the path using this in place of ~
sysdir
适用于 macOS 10.12+对于 macOS 10.12 及更高版本,Apple 提供
sysdir.h
API:sysdir
取代了现已弃用的NSSystemDirectories
和FindFolder
方法。sysdir
for macOS 10.12+For macOS 10.12 and later, Apple provide the
sysdir.h
API:sysdir
replaces the now deprecatedNSSystemDirectories
andFindFolder
approaches.这不是应用程序支持,但您可能不想在那里存储文件。而是使用通过调用“HOME”获得的目录:
您可以使用 C 函数 getenv:
char *home = getenv("HOME");
并使用以下命令获取 C++ 字符串:
字符串(主页)
This is not application support but you probably don't want to store files there anyways. Instead use the directory that you get from calling "HOME":
You can use the C-function getenv:
char *home = getenv("HOME");
and to get the C++ string use:
string(home)