Brandon Miller
Brandon Miller

Reputation: 2297

Win32 Application Settings

How can I store settings that can later be loaded into my application?

For instance, I have an edit control that the user can change the font, font color, and background color of. When the user changes these, I would like to save them so that when the user opens the application again the fonts and colors will be the same as they left them.

I had thought about writing to the windows registry, but not only am I confused as to how to save fonts there, (or even if you could do such a thing), but I also have a portable version that can be run off of a flash-drive. Is there any way to save these settings to a file that is in the same directory as the executable? If so, how would I go about saving and loading these?

If there isn't a way to accomplish this using a settings file, how could I use the windows registry to do it?

Upvotes: 3

Views: 3619

Answers (3)

marcinj
marcinj

Reputation: 49986

Best way IMO is to store it in ini files, for example, one file : app.ini

[AppSettings]
fntBackground=255 255 0
fntFace=Tahome

[LoadDialog]
...

it is quite easy to write it yourself, you can also find ready classes on codeproject, I took a fast search and found this:

http://www.codeproject.com/Articles/5401/CIni

looks promising

Upvotes: 3

SChepurin
SChepurin

Reputation: 1854

The easiest way to read config info in Windows is by using GetPrivateProfileString/WritePrivateProfileString functions as Zdeslav Vojkovic said.

But then you see this: Note This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry. Which is obsolete nowadays either and often is forbidden. Recommendations (but often just simple fashion) require you to use XML files for this. Then you can use whatever method you like - DOM, SAX, XMLLite.

Upvotes: 2

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14591

If your application is allowed to write to the folder, then you can use an .ini file as many Windows application do. You can read and write to the file with GetPrivateProfile/WritePrivateProfile APIs, e.g GetPrivateProfileInt

If you are using some framework, it is possible that it also provides friendlier version of the API (e.g. MFC provides CWinApp::GetProfileInt and others).

Upvotes: 2

Related Questions