Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I avoid problems handling *.ini files? 2

Status
Not open for further replies.

JerryAttrick

Technical User
May 14, 2005
23
NZ
In a small project I’d like to save user settings for the next run. I perceive that: -
1. On exit I need to generate an *.ini file storing the settings.
2. On subsequent runs I read back those settings on entry.

· On the first run, how do I avoid an (imagined) error situation when I try to read a non-existent file?
· On entry to my code can I check whether the *.ini file already exists - if yes then how?
· Do I need to read the file and then delete it every time (each time recreating and replacing it on exit)?
· Can I merely create a new *.ini file each time and write over the top of the old? Won’t that cause errors/problems?
 
why not use the registry, that way you don't have to bother with the filesystem...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
If you want to use the file system u could do the following:

1. Why not just create the ini file and just check every startup whether there is data in it.

2. Creating the ini-file as following :
Code:
var
IniFile  : TIniFile;
begin
IniFile := TIniFile.Create(
               ChangeFileExt('\Settings','.ini'));
this will overwrite the file if it already exsists.
3. Add the lines with your settings:
Code:
  IniFile.WriteString('just some string', 'or strings', VariableString);
  IniFile.WriteDate('Date', Date);
4. Now when you open your app again read the data in ini file:
Code:
IniFile := TIniFile.Create(
             ChangeFileExt('\Settings','.ini'));
    StringS := IniFile.ReadString('just some string', 'or strings','');
Hope this helps you, otherwise just let me know
 
why not use the registry, that way you don't have to bother with the filesystem...
Writing settings in .ini files is much more portable between installations, and can do without 'installation', and it's easier to move an app to another machine, or work with common settings while using a network.

I use combinations of both, local display and form-location settings in the registry, and common parameters etc. in an .ini file like:
Code:
With TIniFileCreate(ChangeFileExt(Application.ExeName,'.ini')) do
Begin
    // save Settings
    ...
    // flush to disk & close
    UpdateFile;
    Free;
End;

HTH
TonHu
 
hi

I have a similar system. Just some ideas:

On an initial run (ie no ini file), create the file with a default section which you use for a user until they exit. I also put up a msg saying 'default settings used'. When the user exits, a section (using their login name) is creating in the file containing their settings. When a user logs in again, it searches the ini for their login name and if it's not found, uses the default one - you could create their section at this point with the default settings and update them on exit.

Are you using a database? If so, you could store settings in there.

I never use the registry as a lot of people 'hot desk' in our place.

Use FileExists to check whether you need to create the file or not.

This is from Delphi help. Don't forget to add Inifiles to your USES clause:
Code:
procedure TForm1.FormActivate(Sender: TObject);
var MyIniFile: TIniFile;
begin
  MyIniFile := TIniFile.Create('myapp.ini');
  with MyIniFile do
  begin
    if ReadBool('AutoLoad', 'FormProperties', False) = True then
    begin
      Visible := ReadBool('FormOptions', 'Visible', True);
      Color := TColor(ReadInteger('FormOptions', 'Color', clNormalBackground));
      Caption := ReadString('FormOptions', 'Caption', 'Main');
  end;
  MyIniFile.Free;
end;

lou
 
Shouldn't you open the file '.\myapp.ini'? because by default Windows puts the .ini file in the C:\Windows ({win}) directory, afaik.

HTH
TonHu
 
hi TonHu

I took that snippet from the Delphi help as a guide. He does need to specify the full path and his filename, like you say.

lou
 

tonhu said:
can do without 'installation', and it's easier to move an app to another machine,
Registry keys can have default values, so there is no reason for an app to be unusable on a new machine even if an installer is not used, and things like file location could be different on another machine anyway.

tonhu said:
or work with common settings while using a network.
Fair enough, but on the sort of network I work with that probobly wouldnt be a good idea.

I think the main disadvantage of ini files is that, the user can get at them and delete them or mess them up.
Plus I have had problems with 'file not found' type errors on an ini file based project we took over recently.




Steve: Delphi a feersum engin indeed.
 
There is no need to check if the ini file exists or not as the TInifile Read functions insist that you specify a default value in case the ini file, the section or the key does not exist.

The same applies if you use the TRegIniFile functions to get at regisry values.

If you use the TRegistry functions to get at registry values then you must check that the section and key exists otherwise you will get an exception.

I use a general purpose function to get the .INI file (which is located in the application's folder).
Code:
//------------------------------------------------------------------------------
function GetIniFile: TIniFile;
//------------------------------------------------------------------------------
begin
  result := TIniFile.Create ( ChangeFileExt ( Application.ExeName, '.INI' ) );
end;
I also use a standard procedure for saving and restoring the position and size of all my forms.
Code:
//------------------------------------------------------------------------------
procedure SaveFormPosition ( form: TForm );
//------------------------------------------------------------------------------
var
  FormName: string;
begin
  with GetIniFile do try
    FormName := 'Form ' + form.Name;
    WriteInteger ( FormName, iniTop, form.Top );
    WriteInteger ( FormName, iniLeft, form.Left );
    WriteInteger ( FormName, iniWidth, form.Width );
    WriteInteger ( FormName, iniHeight, form.Height );
  finally
    free;
  end;
end;
iniTop and so on are constants 'Top', 'Left' and so on.

Andrew
Hampshire, UK
 
My sincere thanks to you all for the assistance.

I now have the ini file working in simple form and can write and read keys OK. It's now just a matter of adding all the other keys I want. I did strike some odd debugging messages on the way which I eventually fixed (mostly 'finger trouble'!). I still have to effect locating the ini file in the application folder, which I'll try to get working next.

Jerry
BOP NZ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top