Mega Code Archive

 
Categories / Delphi / Examples
 

Ini file caching

Personally, I prefer using INI files for saving configuration data of my programs. This gives me an easy way of helping them if I want to prevent that they had to hack around in the dark deeps of the ever-growing Windows registry. Here are two tips from my experiences with using INI files: Save your INI files in the main directory of your program I think that preventing to spread files all over our user's PC's is a good habit. Filling up his or her Windows directory with one more INI file (often forgotten when removing programs !) isn't required anymore if you just open/create your INI file(s) with the line: MyIni := TIniFile.Create(ExtractFilePath(Application.ExeName)+'MyApp.INI'); Opening INI files as text In some cases it could be required that you open your INI file as text. This might lead into problems if you concurrently do this after your file has also been opened using the INI file functions since Windows works on a cached version of the INI file once it was opened. It could happen that your changes won't be saved correctly... I have spent many hours with searching for the reason for those problems until I found the following way to flush Windows' cache: WritePrivateProfileString(NIL,NIL,NIL,'MyIni.INI'); If all three parameters are NULL, the function flushes the cache. Note that the function always returns False after flushing, regardless of whether the flush succeeds or fails.