在现代 Windows 中存储/检索配置数据的最佳方式
我最近没有为 Windows 编写太多代码,我发现自己现在正坐在 Visual Studio 中,用 C++ 为 Windows 7 编写一个小程序。我需要读取/写入一些配置数据。
在过去,(作为一个 Borland 类型的人)我只是使用 TIniFile 并将 .ini 保留在我的 exe 旁边,显然这不再是完成的事情了。 MS 文档告诉我 Get/WritePrivateProfileString 只是为了兼容性,我怀疑这些天我是否能逃脱写入程序文件的麻烦。天哪,我感觉自己老了。
我希望生成的文件易于编辑 - 在记事本之类的东西中打开,并且易于查找。这是一个小应用程序,当我可以编辑配置文件时,我不想编写设置屏幕。
那么,现代的做法是什么呢?
I've not done much coding for Windows lately, and I find myself sitting at Visual Studio right now, making a small program for Windows 7 in C++. I need some configuration data to be read/written.
In the old days, (being a Borland kind of guy) I'd just use a TIniFile and keep the .ini beside my exe Obviously this is just not the done thing any more. The MS docs tell me that Get/WritePrivateProfileString are for compatibility only, and I doubt that I'd get away with writing to Program Files these days. Gosh I feel old.
I'd like the resulting file to be easily editable - open in notepad sort of thing, and easily findable. This is a small app, I don't want to have to write a setup screen when I can just edit the config file.
So, what is the modern way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
人们经常使用 XML 文件来存储首选项,但它们通常太过分了(而且它们实际上对于人类来说并不是那么可读)。
如果您的需求可以通过 INI 文件轻松满足,您可能需要使用 Boost.Program_options 使用配置文件解析器后端,它实际上编写类似 INI 的文件,而无需通过已弃用(且缓慢!)的 API,同时公开一个漂亮的 C++ 接口。
正确的关键是在哪里编写这样的配置文件。正确的位置通常是用户应用程序数据目录的子目录(例如命名为您的应用程序);拜托,拜托,拜托,不要在可执行文件中对其路径进行编码,我已经看到足够多的损坏的应用程序无法理解用户配置文件可能不在
c:\Documents and settings\用户名
。相反,您可以使用 检索应用程序数据路径
SHGetFolderPath
函数与CSIDL_APPDATA
(或SHGetKnownFolderPath
和FOLDERID_RoamingAppData
(如果您不介意失去与 Vista 之前的 Windows 版本的兼容性) ,甚至只是扩展%APPDATA%
环境变量)。通过这种方式,每个用户将能够存储其首选项,并且在编写首选项时不会出现任何与安全相关的错误。
Often people use XML files for storing preferences, but they are often overkill (and they aren't actually all that readable for humans).
If your needs would be easily satisfied with an INI file, you may want to use Boost.Program_options using the configuration file parser backend, which actually writes INI-like files without going through deprecated (and slow!) APIs, while exposing a nice C++ interface.
The key thing to get right is where to write such configuration file. The right place is usually a subdirectory (named e.g. as your application) of the user's application data directory; please, please, please, don't harcode its path in your executable, I've seen enough broken apps failing to understand that the user profile may not be in
c:\Documents and settings\Username
.Instead, you can retrieve the application data path using the
SHGetFolderPath
function withCSIDL_APPDATA
(orSHGetKnownFolderPath
withFOLDERID_RoamingAppData
if you don't mind to lose the compatibility with pre-Vista Windows versions, or even just expanding the%APPDATA%
environment variable).In this way, each user will be able to store its preferences and you won't get any security-related errors when writing your preferences.
这是我的观点(我认为你得到的大多数答案都是观点),但现在的标准做法似乎是将这样的配置文件存储在\AppData\Roaming\。对于单个配置文件来说,这可能有点过分了,但这将使您有机会将所有应用程序数据放在一个位置(如果您添加更多数据)。
C:\Users\中;
。此外,通常最好不要使该目录本身混乱,而是使用子目录来存储应用程序的数据,例如 C:\Users\This is my opinion (which I think most of the answers you get will be opinion), but it seems that the standard way of doing things these days is to store config files like these in
C:\Users\<Username>
. Moreover, it is generally good to not clutter this directory itself, but to use a subdirectory for the purpose of storing your application's data, such asC:\Users\<Username>\AppData\Roaming\<YourApplicationName>
. It might be overkill for a single config file, but that will give you the opportunity to have all of your application data in one place, should you add even more.