Shifu-Hacks Blog

愚かさは博学ではない。

Working with INI files in Delphi

Posted by compactwater on August20 2007

Initialization file! To use these functions, you will need to add ‘INIFiles’ to your uses list. I’ve also used IntToStr and StrToInt in this example. Making and reading an INI file is simple, but be sure you only use it when you have to. If you don’t specify a path for your INI, it will be put in the system folder of the current computer (For me, it’s C:\WINNT), if you want the user to be able to change it without opening the application, you should specify a location for it, usually in the same folder as the application.

So, lets make the INI now. Add MyINI: TIniFile; to your var. If you put it in a procedure or function, you’ll have to redeclare it for every procedure or function you use it in, otherwise it will give you an error Undeclaired identifier: ‘MyINI’. WriteString or WriteInteger can keep you from having to use IntToStr/StrToInt, but WriteString is more commonly used. Here’s what it’ll look like:

  1. MyINI := TIniFile.Create(‘MyINI.ini’);
  2. MyINI.WriteInteger(‘info’, ‘anumber1’, StrToInt(LabeledEdit1.Text));
  3. MyINI.WriteInteger(‘info’, ‘anumber2’, StrToInt(LabeledEdit2.Text));
  4. MyINI.WriteString(‘info’, ‘yourname’, LabeledEdit3.Text);
  5. MyINI.Free;

1. Make/edit the INI file.
2. Write anumber1 to the INI as an integer under info.
3. Write anumber2 to the INI as an integer under info.
4. Write yourname to the INI as a string under info.
5. Free the INI (never forget to do this!).

When finished, the INI will look something like this:

  1. [info]
  2. anumber1=5
  3. anumber2=2
  4. yourname=Kent

[section]
name=value

Now it’s time to load the INI!

  1. MyINI := TIniFile.Create(‘MyINI.ini’);
  2. Labelededit1.Text:=IntToStr(MyINI.ReadInteger(‘info’, ‘anumber1’, 0));
  3. Labelededit2.Text:=IntToStr(MyINI.ReadInteger(‘info’, ‘anumber2’, 0));
  4. Labelededit3.Text:=MyINI.ReadString(‘info’, ‘yourname’, ‘?’);
  5. MyINI.Free;

1. Make/edit the INI.
2. Load anumber1 as an Integer under info, if not found return 0.
3. Load anumber2 as an Integer under info, if not found return 0.
4. Load yourname as a string under info, if not found return ?.
5. Free the INI (never forget to do this!).

Advertisements

7 Responses to “Working with INI files in Delphi”

  1. ludvig said

    Having been here before and lost, to be here and win, I’ve got to tell you, winning is really a lot better than losing. Really a lot better.

  2. Richard said

    How do you get to see the data that has been gathered from an ini file in a label or memo ?

  3. i am repeatedly roaming all over the world-wide-web roughly all of the night thus I posses a trend to study quite a ton, which isnt typically a advantageous factor as many of the web sites I view are made up of useless absurdity cloned from identical web pages a thousand times, but I’ll hand it to ya this web page is absolutely half decent and even possesses a large amount of legitimate written content, for that purpose many many thanks for striking the pattern of entirely replicating other individuals websites.

  4. Sowmiya said

    This WEbsite is verry verry usefull to us

  5. Athira said

    The webpage was relatively easy to follow. Everything was broken down in categories so it was easy to manoeuvre your way through the site.

  6. Rathika said

    The website was extremely up to date including upcoming events on dates.

  7. This website really very useful for us………

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: