应用程序设置方法? c++
我正在考虑向应用程序添加可配置设置,我认为最简单的方法是外部文件或 win 注册表(它是仅限 win 的应用程序)。
哪种方式更好?
我想知道,没有足够权限的用户可能无法创建/写入配置文件。就注册表而言,今天的防病毒软件允许我添加/编辑/删除注册表项吗?或者他们只监控某些按键?
另外,如果有人知道在 vc++ 中管理配置设置(在纯 win32 中)的类/lib,请发布它。
I am thinking about adding configurable settings to an application, and I think the easiest ways are an external file or win registry (its a win only app).
Which way would be better?
I was wondering, an user with not enough permissions may not be able to create/write the config file. And in the case of the registry, would todays antivirus allow me to add/edit/remove keys? Or they only monitor certain keys?
Also, if someone knows a class/lib to manage config settings (in pure win32) in vc++ please post it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“仅限 Windows”是限制还是限制解除?如果您不介意跨平台,那么我建议您给出
boost::program_options
尝试一下。该库通过命令行、环境变量和 INI 文件支持程序选项。 Boost 的program_options
还通过variables_map
很好地集成和粘合了各种解析器,您可以将其视为选项及其值之间的映射。Is "Windows-only" a restriction or a restriction-relief? If you don't mind being cross-platform then I suggest you give
boost::program_options
a go. The library supports program options through commandline, through evironment-variables and through INI files. Boost'sprogram_options
also integrates and glues the various parsers very nicely withvariables_map
, which you can view as a map between options and their value.对于简单的事情,您不妨使用注册表。但是,配置文件有很多好处......您可以为应用程序的不同用途保存/加载多个不同的配置,更容易在用户或计算机之间共享或迁移设置等。
如果您最终选择文件路径,我会推荐Boost的Property Tree库:
http://www .boost.org/doc/libs/1_41_0/doc/html/property_tree.html
它有一个非常好的语法:
它还支持读取和写入各种格式,例如 xml 等。
For simple stuff, you might as well just use the registry. However, there are many benefits to a config file... you can save/load several different configs for different uses of your app, it's easier to share or migrate settings between users or machines, etc.
If you end up going the file route, I would recommend Boost's Property Tree library:
http://www.boost.org/doc/libs/1_41_0/doc/html/property_tree.html
It has a pretty nice syntax:
It also supports reading and writing to various formats, like xml and others.
我认为新的默认设置是在用户文件夹下的用户“AppData”文件夹中写入一个配置文件,该文件应该可以安全地写入/读取。
我们使用一个简单的 XML 格式的文件来存储设置;但您可以使用 INI 文件类型格式。
I think the new default thing is to write a configuration file in the user's "AppData" folder under the user's folder which should be safe to write/read from.
We're using a simple XML formatted file to store settings; but you could use a INI file type formatting.
如果您使用
CSIDL_COMMON_APPDATA
将配置文件保存在应用程序数据目录SHGetFolderPath()
中,所有用户都将能够看到该配置。如果您使用CSIDL_LOCAL_APPDATA
,则只有一个用户能够看到该配置。注册表不一定是保存所有配置数据的地方。If you save your configuration file in the Application Data directory
SHGetFolderPath()
withCSIDL_COMMON_APPDATA
all users will be able to see the configuration. If you useCSIDL_LOCAL_APPDATA
then only the one user will be able to see the configuration. The registry is not necessarily the place to save all configuration data.据我所知:
您应该能够在用户的“主目录”或“应用程序数据”目录中创建文件,无论权限如何。通常这些目录应该是可写的。
从未见过我的防病毒软件干扰注册表操作。只要您不在注册表中做任何可疑的事情,您可能就没事。
这是品味问题。我认为文本文件更好 - 允许更轻松地迁移设置。只是卸载后不要留下垃圾。
另外,如果有人知道在 vc++ QSettings Qt 4。但是仅仅使用整个 Qt 来保存设置绝对是一种矫枉过正。您还可以检查 JSON 等配置语言,使用 lua 进行设置(比使用 Qt 4 更省力)或获取任何 XML 库。此外,直接使用注册表或使用 iostreams 或 stdio 编写配置文件应该不难。如果您愿意,您随时可以编写自己的配置库。
As far as I know:
You should be able to make files inside user's "home directory" or "application data" directory, regardless of permissions. Normally those directories should be writeable.
Haven't ever seen my antivirus interfere with registry manipulation. You probably will be fine as long as you aren't doing anything suspicious in registry.
It is matter of taste. I think that text file is better - allows easier migration of settings. Just don't leave junk behind after uninstall.
QSettings in Qt 4. But using entire Qt for just saving settings is definitely an overkill. You could also check configuration languages like JSON, use lua for settings (less overkill than using Qt 4) or get any XML library. Also, working with registry directly or writing configuration files using iostreams or stdio shouldn't be hard. And you can always write your own configuration library - if you feel like it.