【问题标题】:How to locate application preferences folder using OS X APIs?如何使用 OS X API 定位应用程序首选项文件夹?
【发布时间】:2014-09-04 11:45:40
【问题描述】:

我遇到了一个(过时的)代码 sn-p,它使用文件管理器功能(包含在 Carbon 中)来定位共享应用程序的 Preferences 文件夹(在大多数情况下只是“/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) 的功能。

因此我想知道:找到应用程序首选项文件夹的当前正确方法是什么(没有将“库/首选项/应用程序名称”硬连接到代码中)?谢谢

【问题讨论】:

标签: c++ macos osx-mavericks


【解决方案1】:

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

【讨论】:

  • 最初我想使用纯 C++ 接口(所以没有 NS-wathever)。经过一些研究,在我看来,不再提供这样的东西,所以更好的方法是为我的 C++ 代码编写一个简单的 Objective-C++ 扩展。您的回答为我指明了正确的方向。
  • 从技术上讲,您可以在纯 C 中通过直接询问 IMP 的运行时然后调用它来实现。但这相当于在没有 Objective-C 语法的情况下使用 Objective-C 方法——它会冗长且丑陋。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
相关资源
最近更新 更多