我遇到了一个(过时的)代码片段,它使用文件管理器功能(包含在 Carbon 中)来定位共享应用程序首选项文件夹(在大多数情况下只是“/Library/Preferences”)。它是这样的:
#include <Carbon/Carbon.h>
...
FSSpec spec = { 0 }; // data type which specifies name and location of a file or folder
FSRef ref; // data type which references in some sense a file or a folder
OSErr err = fnfErr;
// find a 'preferences' type folder (specified by kPreferencesFolderType)
// with the necessary permissions (specified by kUserDomain)
err = FindFolder(kUserDomain, ,
1, &spec.vRefNum, &spec.parID);
// operate some conversions to put the folder path inside a string where you can
// then append the app name to it.
FSpMakeFSRef(&spec, &ref);
FSRefMakePath(&ref, (UInt8*)filename, FL_PATH_MAX);
(kUserDomain
和 kPreferencesFolderType
是在 CarbonCore/Folders.h 中定义的枚举值。)
不幸的是,许多“FS”文件管理器功能似乎已被弃用,尤其是那些使用 FSSpec
(https://developer.apple.com/library/mac/documentation/Carbon/reference/File_Manager/Reference/reference.html) 的功能。
因此我想知道:当前定位应用程序首选项文件夹的正确方法是什么(无需将“Library/Preferences/AppName”硬连接到代码中)?谢谢
最佳答案
我敢想象你想要使用带有NSSearchPathDirectory
的东西——推测NSFileManager -URLsForDirectory:inDomains:
,可能与 NSLibraryDirectory
一起,您需要自己添加 Preferences
位。根据 Apple 公开的那些选项,我真的不认为你打算自己从路径中读取和写入 - 它只是为了用户默认设置,公开的 NSApplicationSupportDirectory
就是你想要的地方抛出其他支持数据。
关于c++ - 如何使用 OS X API 定位应用程序首选项文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24738575/