Important alert: (current site time 7/16/2013 4:38:41 AM EDT)
 

article

TINIFile And You - Using INI Files in Delphi

Email
Submitted on: 1/19/2002 1:42:31 AM
By: bleh 
Level: Beginner
User Rating: By 23 Users
Compatibility: Delphi 5, Delphi 4
Views: 83479
 
     Learn the magic of the TINIFile object.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
TINIFile And You - Using INI Files in Delphi

This article is just a simple explanation of how to implement the TINIFile object into your Delphi application. Thankfully, the wonderful people at Borland have created this object for us, so we don't have to actually write out the API to do so (such as in VB). If you already understand what an INI File is and how it works, you can skip directly to Part 2. Otherwise, I suggest you read Part 1 first

Part 1: Understanding INI Files

INI Files are nothing more than text files that are used by many programs to store application and operating system values, such as a programs window position, colors, or virtually any other settings. INI Files were rather abundant in Windows 3.1, and to alleviate this growing problem, Microsoft decided that with Windows 95, it would implement a database they so lovingly called the registry (there is a good tutorial on here for the Windows Registry in Delphi, so I won't bother getting into it here). Despite this fact, INI Files are still very much in use within the Windows operating system. A good example is the win.ini file, which can be found in C:\Windows\win.ini. We are going to take a look at this file to help us understand more about it. So to help us out more, open up the file C:\Windows\win.ini in Notepad.

NOTE: It's probably NOT a good idea to change any of the settings in this file, as you can screw up your windows settings if you do. Your best and safest bet is to copy the win.ini file to another location, and open it up from there. Don't blame me if you dink around with this file and something goes wrong on your machine. You've been warned.

An INI File consists of three main part. The Section, the Key, and the Value. If you browse through the win.ini file, you will see that it is split up into "Sections". Each Section is defined by brackets. Beneath each Section is a list of "Keys", and their respective "Values". Here is an example and explanation of some Keys and Values from the Desktop Section of my win.ini file.

[Desktop]
Wallpaper=(None)
TileWallpaper=0
WallpaperStyle=2
Pattern=(None)

In this example, our Section is called "Desktop". Beneath this, we have the Keys "Wallpaper", "TileWallpaper", "WallpaperStyle", and "Pattern". Their respective Values are "(None)", "0", "2", and "(None)". Your values are probably different than mine, but the Keys should be the same. Every time Windows loads, the Operating System checks this file, and creates your Desktop settings based on the values within this Section.

I can't really think of anything else to write to explain this any better, so I am going to just jump ahead to using the TINIFile object within your Delphi project.

Part 2: TINIFile Object - Borland Loves You

Like many programmers, I once too struggled with the unholy carnage that was using the Windows API in Visual Basic. Then one day, I heard about Delphi. After messing around with a copy of it at a friends house, I found that all the things that were absolutely annoying and tedious about Visual Basic could be done about 100 times faster and more efficiently in Delphi. I had seen the light at the end of the tunnel, and it was being tended by the good people at Borland. They took the lame task of dealing with INI Files and put it all into one, easy to use object.

The first thing we are going to do to enable the ability to read and write to INI Files in our project is to put the word "INIFiles" into the Uses section of our main unit. Example:

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, INIFiles
;

Let's go ahead and put a text box and two buttons onto our form. Name the text box "txtValue". Name the Buttons "btnRead" and "btnWrite", and set their Captions to "Read INI" and "Write INI" respectively. Create an "onClick" event for your btnWrite button, and insert the following code.

TForm1.btnWriteClick(Sender: TObject);
var
    myINI : TINIFile;
begin
    myINI := TINIFile.Create(ExtractFilePath(Application.EXEName) + 'myinifile.ini');
    myINI.WriteString('Settings', 'Text Box', txtValue.Text);
    myINI.Free;
end;

The first thing this code does is assign myINI as a TINIFile object. The .Create() is telling us that "Hey, everything within these parentheses is the path to our INI File." I have used "ExtractFilePath(Application.EXEName) + 'myinifile.ini'". This is telling us that our INI File will be located in the application's directory. Feel free to change this path if you wish. If the file doesn't exist, don't worry. Delphi will automatically create the INI File once we call our next bit of code.

The second thing this code does, is to write out a Value to a specific Key within a certain Section. The Section we are writing to here is 'Settings', although realistically, it could be called anything you want. The second parameter is telling Delphi that we want to write out a Key called 'Text Box'. Again, this can be anything you want it to be. Finally, the last parameter is our Value for the Key 'Text Box', in the Section 'Settings'. In this example, it will write out whatever is contained within txtValue.Text.

Now that we have successfully written to a INI File, let's read it back in. Create an "onClick" even for your btnRead button and insert the following code. :

TForm1.btnReadClick(Sender: TObject);
var
    myINI : TINIFile;
begin
    myINI := TINIFile.Create(ExtractFilePath(Application.EXEName) + 'myinifile.ini');
    txtValue.Text := myINI.ReadString('Settings', 'Text Box', 'Default');
    myINI.Free;
end;

Basically, the first line of code does the same thing as the btnReadClick event. If the file doesn't exist, don't worry. The next line of code is a bit different than the WriteString procedure, however. Like the WriteString procedure, it is going to look for the Section 'Settings', and the Key 'Text Box'. However, our third parameter is different. This parameter is what will be returned if the Key 'Text Box' or Section 'Settings' doesn't exist. It will also return this if there is no INI File to begin with. So if the Key exists, whatever is contained within it will be placed into the txtValue. If it doesn't, then the word 'Default' will be put there.

Part 3: Other Types Of Procedures and Functions..

In addition to reading and writing strings to/from INI Files, we can also read/write Integers, using "ReadInteger()" and "WriteInteger()" respectively. There are also a slew of other procedures and functions that we can use.

  • DeleteKey
  • EraseSection
  • ReadBinaryStream
  • ReadBool
  • ReadDate
  • ReadDateTime
  • ReadFloat
  • ReadSection
  • ReadSections
  • ReadBinaryStream
  • ReadSectionValues
  • ReadTime
  • SectionExists
  • WriteBinaryStream
  • WriteBool
  • WriteDate
  • WriteDateTime
  • WriteFloat
  • WriteTime
  • UpdateFile
  • ValueExists

Hopefully this will help you along your way with using INI Files. If you want an indepth description of what each function, procedure, method, or event does, check the Delphi Help files.


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/19/2002 2:05:21 AMKush Desai

Well written. Keep up the good work.
(If this comment was disrespectful, please report it.)

 
6/18/2002 4:13:03 AMBree

very well done, yes please :) keep up the good work.
(If this comment was disrespectful, please report it.)

 
8/5/2002 12:44:05 PMYoudao

I have Delphi 4 app which uses TiniFile component. The WriteString method generate an error on Windows XP Professional workstation (Read is OK on XP). On Windows 98, it works ok. Does TiniFile support XP?

Thanks a lot
(If this comment was disrespectful, please report it.)

 
9/21/2002 4:59:32 PM

Excellent, and well-timed :) I found it just when I needed it.
Didn't I see your name on a book spine somewhere?
(If this comment was disrespectful, please report it.)

 
9/28/2002 8:29:30 PMCharles Chadwick

I doubt you did, but that would be pretty cool =).
(If this comment was disrespectful, please report it.)

 
11/9/2002 3:35:01 PM

Nice simple article, thanks. Perhaps you (unnamed) thinking of Tomes of Delphi, co-Author Charles Calvert :)
(If this comment was disrespectful, please report it.)

 
1/14/2003 3:29:16 PMWayne & Carr Barron

Thank you, I needed this information, as I needed in a New Program that I have developed, and this finished the 2nd release of it.
Just hopet that it works in XP, Will find out in a few, As the first customer is my Father and he runs XP...So take care and thank you

Wayne & Carr Barron
Carrz-Fox-Fire Promotions
http://www.carrz-fox-fire.com
Delphi Fire Security Resources
http://delphi.carrz-fox-fire.com
(If this comment was disrespectful, please report it.)

 
3/4/2003 4:48:10 PM

Excellent code. Explained well. Thanks
(If this comment was disrespectful, please report it.)

 
4/29/2003 6:08:32 PM

cool, i never have seen before an article like this, it's really good for ppl who doens't know how to work with .INI files in delphi.
(If this comment was disrespectful, please report it.)

 
5/2/2003 4:40:20 PMScott Armstrong

Perfect for me because I have no clue how to use the registry, plus, the ini files that I want to use get recreated whenever the program is run anyway. Great tutorial.
(If this comment was disrespectful, please report it.)

 
6/20/2003 4:59:31 AM

It should work okay in Win XP and Win 2000 but you may have access rights problems if you are not the administrator. It all depends on if you have the right to write(!?). Once you have mastered INI file manipulation, Borland have kindly made it really easy to convert your code to update the registry instead of a file. See the Delphi help for more info.
Great article.
(If this comment was disrespectful, please report it.)

 
7/15/2003 7:23:19 AM

Thanks a lot, i've been trying for a lot of time to use the tinifile object, and it never worked because i didn't know the use statement.
thanks again, and gretings from Romania!
(If this comment was disrespectful, please report it.)

 
9/30/2003 7:32:42 PM

Wow, this is fantastic and is definantly superior to the visual basic methods. Long live delphi :)
(If this comment was disrespectful, please report it.)

 
10/5/2003 1:29:55 PMslushey

Three words: Oh my god.

R0x0r!
(If this comment was disrespectful, please report it.)

 
10/8/2003 8:15:42 PM

Excellent.
I did already write a good functioning procedure myself (it basically opens the file, and searches line by line until found or not). But this is a standardized approach.
(If this comment was disrespectful, please report it.)

 
1/11/2004 1:10:19 PM

Brilliant stuff
(If this comment was disrespectful, please report it.)

 
2/14/2007 7:57:57 PMGeorgi Angelov

Actually this doesn't work if you have two forms! When you load another form somethings happens and it doesn't want to write into file i don't know why !
(If this comment was disrespectful, please report it.)

 
4/20/2007 3:46:17 AMAli Ezzeddine

Very nice and easy, thanks Charles

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.