如何使用OS X api定位应用程序首选项文件夹

How to locate application preferences folder using OS X APIs?

本文关键字:应用程序 首选项 文件夹 定位 api 何使用 OS      更新时间:2023-10-16

我遇到了一个(过时的)代码片段,它使用文件管理器函数(包含在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);

(kUserDomainkPreferencesFolderType是在CarbonCore/Folders.h.中定义的枚举值)

不幸的是,许多"FS"文件管理器功能似乎被弃用了,特别是那些使用FSSpec (https://developer.apple.com/library/mac/documentation/Carbon/reference/File_Manager/Reference/reference.html)的功能。

我因此想知道:这将是当前正确的方式来定位应用程序首选项文件夹(没有硬接线"库/首选项/AppName"到代码)?由于

我敢想象你想使用一些需要NSSearchPathDirectory的东西-可能是NSFileManager -URLsForDirectory:inDomains:,可能是NSLibraryDirectory,你需要自己添加Preferences位。基于Apple公开的这些选项,我真的不认为您打算自己从路径中读取和写入—它只是用于用户默认值,暴露的NSApplicationSupportDirectory是您只需扔其他支持数据的地方。