3

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?

Brandon Miller
  • 2,247
  • 8
  • 36
  • 54
  • If you can use boost libraries, you should review [`boost::property_tree`](http://www.boost.org/doc/libs/1_44_0/doc/html/property_tree.html#boost_propertytree.intro) library. I have never used this, but seems to be what you are after. – hmjd Oct 10 '12 at 12:52

3 Answers3

3

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

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Yes, looks very promising. Now, this maybe a whole other question but lets say the .ini file is beside my executable, which is in `C:\Program Files\MyProgram`, how would I get access to write to it? And what if the user can't give the application administration privileges? I would like to still be able to run my program but I guess just not save the settings when they are changed. – Brandon Miller Oct 10 '12 at 17:20
  • I can do without that question being answered if you don't have one, but one thing I really want to know is: Can instead of writing a `int`, `bool`, or `char` to the INI file, can I write a `LOGFONT` structure to it? If not I would have to save every member of the `LOGFONT` structure to a different key and somehow deal with converting the `long`s and `BYTE`s to integers. – Brandon Miller Oct 10 '12 at 17:37
  • 1
    store settings in application data folder, to get its path look here: http://stackoverflow.com/questions/2899013/how-do-i-get-the-application-data-path-in-windows-using-c. – marcinj Oct 10 '12 at 19:40
  • 1
    LOGFONT - it would be safier to store each field separately, but this CIni class provides CIni::WriteDataBlock(..) method that allows you to store/load binary data, you would only have to specify pointer to your LOGFONT object as lpData parameter. But in my opinion thats unsafe. – marcinj Oct 10 '12 at 19:48
  • Great stuff, thank you. How would I go about making sure `(APPDATADIR)//MyCompany//MyProgram//1.0//Settings.ini` is created if it doesn't already exist? – Brandon Miller Oct 10 '12 at 20:51
  • I looked at `CreateFile()` but I don't know if it creates the directory, or what parameters to use just to create the blank file so that my `CIni` object can use it. – Brandon Miller Oct 10 '12 at 20:52
2

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).

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
2

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.

SChepurin
  • 1,814
  • 25
  • 17