windows 在没有注册表的情况下存储应用程序数据/设置的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4279532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:40:17  来源:igfitidea点击:

Methods of storing application data/settings without the registry?

c++cwindowswinapi

提问by Kaije

I need some methods of storing and getting data from a file (in WIN32 api c++ application, not MFC or .NET)

我需要一些从文件中存储和获取数据的方法(在 WIN32 api c++ 应用程序中,而不是 MFC 或 .NET)

e.g. saving the x, y, width and height of the window when you close it, and loading the data when you open the window.

例如,在关闭窗口时保存窗口的 x、y、宽度和高度,并在打开窗口时加载数据。

I have tried .ini files, with the functions -- WritePrivateProfileString and ReadPrivateProfileString/Int, but on MSDN it says

我已经尝试过 .ini 文件,其功能是——WritePrivateProfileString 和 ReadPrivateProfileString/Int,但在 MSDN 上它说

"This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry."

“提供此功能只是为了与基于 16 位 Windows 的应用程序兼容。应用程序应将初始化信息存储在注册表中。”

and when i tried on my Windows7 64bit machine to read a ini file, i got blue screen! (in debug mode with visual studio) O.O

当我尝试在我的 Windows7 64 位机器上读取 ini 文件时,出现蓝屏!(在 Visual Studio 的调试模式下)OO

I notice that most other application use XML to store data, but I don't have a clue how to read/write xml data in c++, are there any libraries or windows functions which will allow me to use xml data?

我注意到大多数其他应用程序使用 XML 来存储数据,但我不知道如何在 C++ 中读取/写入 xml 数据,是否有任何库或 Windows 函数允许我使用 xml 数据?

Any other suggestions would be good too, thanks.

任何其他建议也很好,谢谢。

回答by Dialecticus

There is nothing wrong with .ini files, the only problem with them is where to write them. CIniFilefrom CodeProject is good enough class. Ini file should be placed in %APPDATA%/<Name Of Your Application>(or %LOCALAPPDATA%\<Same Name Here>, as described below).

.ini 文件没有任何问题,它们唯一的问题是在哪里编写它们。CodeProject 中的 CIniFile已经足够好了。Ini 文件应放置在%APPDATA%/<Name Of Your Application>(或%LOCALAPPDATA%\<Same Name Here>,如下所述)。

EDIT: If we are talking about Windows family of operating systems from Windows 2000 onward then function SHGetFolderPathis portable way to retrieve user specific folder where application configuration files should be stored. To store data in romaing folder use CSIDL_APPDATA with SHGetFolderPath. To store data to local folder use CSIDL_LOCAL_APPDATA.

编辑:如果我们谈论的是从 Windows 2000 开始的 Windows 系列操作系统,那么函数SHGetFolderPath是一种可移植的方式来检索应存储应用程序配置文件的用户特定文件夹。要将数据存储在 Romaing 文件夹中,请使用 CSIDL_APPDATA 和SHGetFolderPath. 要将数据存储到本地文件夹,请使用 CSIDL_LOCAL_APPDATA。

The difference between local and roaming folder is in the nature of the data to be stored. If data is too large or machine specific then store it in local folder. Your data (coordinates and size of the window) are local in nature (on other machine you may have different resolution), so you should actually use CSIDL_LOCAL_APPDATA.

本地文件夹和漫游文件夹之间的区别在于要存储的数据的性质。如果数据太大或特定于机器,则将其存储在本地文件夹中。您的数据(窗口的坐标和大小)本质上是本地的(在其他机器上您可能有不同的分辨率),因此您实际上应该使用 CSIDL_LOCAL_APPDATA。

Windows Vista and later have extended function SHGetKnownFolderPathwith its own set of constants, but if you seek compatibility stick to the former SHGetFolderPath.

Windows Vista 和更高版本具有SHGetKnownFolderPath带有自己的一组常量的扩展功能,但如果您寻求兼容性,请坚持使用前者SHGetFolderPath

回答by casablanca

TinyXMLis a popular and simple XML parser for C++.

TinyXML是一种流行且简单的 C++ XML 解析器。

Apart from that, you can really use any format you want to store your settings, though it's considered good practice to keep settings in text format so that they can be hand-edited if necessary.

除此之外,您真的可以使用任何您想要存储设置的格式,尽管将设置保存为文本格式被认为是一种很好的做法,以便在必要时可以手动编辑它们。

It's fairly simple to write your own functions for reading/writing a file in INI or similar format. The format is entirely up to you, as long as it's easily comprehensible to humans. Some possibilities are:

编写自己的函数来读取/写入 INI 或类似格式的文件是相当简单的。格式完全取决于您,只要人类易于理解即可。一些可能性是:

; Comment
# Comment
Key = Value (standard INI format)
Key Value
Key: Value

回答by Steve Townsend

You could use Boost.PropertyTreefor this.

您可以为此使用Boost.PropertyTree

Property trees are versatile data structures, but are particularly suited for holding configuration data. The tree provides its own, tree-specific interface, and each node is also an STL-compatible Sequence for its child nodes.

属性树是通用的数据结构,但特别适合保存配置数据。树提供了自己的、特定于树的接口,并且每个节点也是其子节点的 STL 兼容 Sequence。

It supports serialization, and so is well-suited to managing and persisting changeable configuration data. There is an example hereon how to load and save using the XML data format that this library supports.

它支持序列化,因此非常适合管理和持久化可变配置数据。有一个例子在这里就如何加载和保存使用XML数据格式,该库支持。

The library uses RapidXML internallybut hides the parsing and encoding details, which would save you some implementation time (XML libraries all have their idiosyncracies), while still allowing you to use XML as the data representation on disk.

该库在内部使用 RapidXML,但隐藏了解析和编码细节,这将为您节省一些实现时间(XML 库都有其特性),同时仍然允许您使用 XML 作为磁盘上的数据表示。

回答by Leo Davidson

While INI files may not be the best format, I think you can safely ignore the warning MSDN puts on WritePrivateProfileString and ReadPrivateProfileString.

虽然 INI 文件可能不是最好的格式,但我认为您可以放心地忽略 MSDN 对 WritePrivateProfileString 和 ReadPrivateProfileString 的警告。

Those two functions are NEVER going away. It would break THOUSANDS of applications.

这两个功能永远不会消失。它会破坏成千上万的应用程序。

That warning has been there for years and I suspect was added when the registry was all the rage and someone naively thought it would one day completely replace INI files.

该警告已存在多年,我怀疑是在注册表风靡一时并且有人天真地认为有一天它会完全取代 INI 文件时添加的。

I might be wrong but it would be very unlike Microsoft to break so many existing apps like this for no good reasons. (Not that they do not occasionally break backwards compatibility, but this would cause huge problems for zero benefit.)

我可能是错的,但微软会无缘无故地破坏这么多现有的应用程序,这与微软非常不同。(并不是说它们偶尔不会破坏向后兼容性,但这会导致零收益的巨大问题。)

回答by Artyom

Ohhh My GOD? Have you ever thought of stright-forward solution rather then thinking of Super-Duper-all-can-do framework way?

哦,天哪?你有没有想过直接的解决方案,而不是想着 Super-Duper-all-can-do 框架的方式?

Sorry...

对不起...

You want to store two numbers between restarts???

你想在重启之间存储两个数字???

Save: Open a file, write these two numbers, close the file:

保存:打开一个文件,写入这两个数字,关闭文件:

std::ifstream out(file_name);
out << x << ' ' << y;
out.close();

Load: Open a file, read these two numbers, close the file:

加载:打开一个文件,读取这两个数字,关闭文件:

std::ifstream in(file_name);
if(!in) return error...
in >> x >> y;
if(!in) return error...
in.close();

回答by Muhammad Bilal Shafi

Libconfigis the best solution in C++ as far as I have tried. Works multi platform with minimum coding. You must try that!

就我尝试过的而言,Libconfig是 C++ 中最好的解决方案。以最少的编码在多平台上工作。你一定要试试!

回答by DumbCoder

libxml2. I have seen quite a lot places where it is used. Easy to use and loads of examples to get you started and not a vast library as such. And in C, take it wherever you want.

库 xml2。我见过很多使用它的地方。易于使用和大量示例可以帮助您入门,而不是像这样的庞大库。在 C 语言中,可以随心所欲地使用它。

回答by Luis G. Costantini R.

pugixmlis another good (and well documented) XML parser library. And If you like portability XML is a better option.

pugixml是另一个很好的(并且有据可查的)XML 解析器库。如果您喜欢可移植性,XML 是更好的选择。