Mega Code Archive

 
Categories / Delphi / Examples
 

Save configuration in ini files or the registry

There are 2 quite easy methods to save configuration data of your program: using INI files (all Windows versions) or the Windows registry (for 32 bit programs). If your program (or external parts of your program) have to run under Windows 3.x, too, this is already my first argument why I'm always preferring INI files against the registry. Another reason is easier maintenance: imagine having to tell a computer-newbie how to change a damaged part of his program configuration in the registry during a support call - horrible! Other reasons are much easier backup of the program including program configuration, easiest overview about the saved settings, easy "low-level" editing capabilities of the configuration (Notepad is enough). A commonly underestimated part is that INI files won't "blow up" your registry - as you might know, Windoze doesn't really delete registry keys as they are "deleted" -> it just "hides" them from your view. This leads to registry files of 2-5 MB after a year of happily installing and "uninstalling" one application after each other.... Unfortunately, there are still "programmers" out there who even use the registy to save large amounts of binary data or even localized strings for their complete applications (!) in the registry .. aargh ... let's talk about nicer things again. My personal favourite for opening the INI files is: MyIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); This has the advantage that the INI file will always "follow" the program, even if I change the name of the EXE file. There also exist 2 possible traps when using INI files: Windows 95/98 caches INI data. This might lead to annoying situations if you want to access the INI file as a text file while it is opened or something like that. In this case, add a TIniFile.UpdateFile; to your code which will flush the INI data to disk (Delphi 4. With earlier versions, you can use TIniFile.WriteString(nil,nil,nil);.) INI files bigger than 32kb. If there is much data to save, Windows is overwhelmed ;-). You will have to use a 3rd party component like TIniFile32 or BigIni.zip (get it at the Delphi Super Page) to read and write INI files of this size. Issues which could lead to a decision against the use of INI files are: speed (using the registry is faster due to its structure), better organization (tree structure), if you want to hide something (it's easier to hide a "secret" registration string "somewhere" in the registry than in an INI file, even if it's saved in another directory -> for better tips please read my Anti-Cracking FAQ) and the M$ Guidelines which suggest to just use the registry under 32bit-Windows. Well, Bill's wishes and visions have never influenced me too much, so I could easily resist until now. ;-)