c++ - 应用程序设置

标签 c++ windows settings

什么是存储应用程序设置的相当标准的方式,主要用于 Windows,但也很容易转移到其他平台。

我基本上有 4 组设置:

  • 全局设置,影响所有用户,并且可以在机器之间移动
  • 全局系统设置,影响所有用户,但特定于该系统(例如该系统的默认设置,例如图形选项)
  • 用户设置,在系统之间移动的用户设置(例如音量)
  • 用户系统设置,特定于该系统的用户设置(例如,依赖于硬件的图形选项)

每个级别都会覆盖前一个级别,允许“全局设置”在很大程度上是应用程序的默认设置,用户设置存储用户选择的选项。 前两个基本上是没有用户设置的默认值(例如对于新用户)。

我考虑过实现一组功能,然后我可以为不同的系统实现这些功能(可能通过 ini 文件),但这是最好的方法吗?

(c++)

namespace config
{
    void Init(const std::string &AppName);
    //updates config for keys/sections that don't exist (ie don't overwrite changes by advanced users by rewriting the entire file)
    void Defaults          (std::map<std::string,std::map<std::string,std::string> > &Map);
    void SystemDefaults    (std::map<std::string,std::map<std::string,std::string> > &Map);

    void Set               (const std::string &Section, const std::string &Key, const std::string &Value);
    void SetSystem         (const std::string &Section, const std::string &Key, const std::string &Value);

    void SetUser           (const std::string &Section, const std::string &Key, const std::string &Value);
    void SetUserSystem     (const std::string &Section, const std::string &Key, const std::string &Value);

    std::string GetValue   (const std::string &Section, const std::string &Key);
}

我知道 Windows 有一组用于此类设置的目录,但这些目录是否适合我的需要?

编辑:我宁愿使用文件(ini 或 xml),也不愿使用 Windows 注册表。然而,将这些配置文件放在每个操作系统下的最佳位置在哪里?

在 Vista 下我找到了这些,它们似乎适合我的组,但是旧的 windows 版本呢(我需要支持 win2000、XP 等),mac/linux 是否有自己的类似文件夹?

  • 全局设置 - \Users\Default\Appdata\Roaming
  • 全局系统设置 - \Users\Default\Appdata\Local
  • 用户设置 - \Users\\AppData\Roaming
  • 用户系统设置 - \Users\\AppData\Local

最佳答案

如果您是 boost 用户,您可以查看 program options库,它支持使用配置文件以及环境变量和(当然)命令行选项。

它被设计成可移植的,因此应该可以减轻您的跨平台头痛。

关于c++ - 应用程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/208475/

相关文章:

c++ - C++11 中的延迟初始化顺序

c# - 将 C# 集成到 C++ 中

c++ - Boost.lockfree 在 RHEL 5.3 上的性能

python - 如何使用 python opencv2 在 Windows 中的图像上写入文本

c++ - Qt Creator C1083 : Cannot open include file: 'cstddef' : No such file or directory

jquery-ui - jQuery UI 的全局选项

ios - 通过自定义应用程序在 iOS 10.1 中打开设置应用程序

c++ - 如何找出导致 "cv::Exception at memory location"的原因?

c++ - 在哪里可以找到适用于 Windows 的视频 (VFW)

wordpress - 为 WordPress 自定义帖子类型创建自定义设置页面