[Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Lukasz Sokol el.es.cr at gmail.com
Tue Mar 15 10:05:41 CET 2011


On 15/03/2011 04:25, Peter Williams wrote:
> 
> Hi All, 
> I use this procedure to save an inifile using the standard IniFiles unit. The problem I have is that when an .ini file already exists I cannot overwrite it. I need the TextFiles equivalent of Rewrite() for Inifiles.
> This line:  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));... when the ini file exists fails with an error message of 'unable to create appname.ini'. This is because it cannot create an ini file which already exists.
> How then do I rewrite the .ini file?
> Uses IniFiles;
> procedure TMain_Form.SaveIniFile;var  ConfigIni: TIniFile;  x, y: integer;begin  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));  // e.g. fred.exe becomes fred.ini  try    for x := 1 to max_pats do      begin        if patterns_list[x].Count > 0 then          begin            ConfigIni.WriteString('Pattern' + inttostr(x), 'Name', Patterns_list[x].Strings[0]);            for y := 1 to patterns_list[x].Count - 1 do              ConfigIni.WriteString('Pattern' + inttostr(x), 'Line' + inttostr(y), Patterns_list[x].Strings[y]);          end;      end;
>     ConfigIni.WriteInteger('General', 'Auto Generation Delay', Timer1.Interval);    if current_color_scheme = krgy then      ConfigIni.WriteString('General', 'Current Color Scheme', 'krgy')    else      ConfigIni.WriteString('General', 'Current Color Scheme', 'kmcw');    ConfigIni.WriteBool('General', 'Sound Wanted', Sound_wanted);
>     ConfigIni.UpdateFile;  finally    ConfigIni.Free;  end;end;{---------------------------------------------------------}
> Best Regards,Peter / pew 		 	   		  
> 
> 

{$ define rewrite } ?

If by 'rewrite' you mean 'to clear it and then write it again', then I'd just plain DeleteFile, then create
it again.
if by 'rewrite' you mean 'to load its contents and_then clear them and_then write new content' then
I'd use the TMemIniFile that can load contents from TString[s][list]. 

L.





More information about the Lazarus mailing list