Quick Search for:  in language:    
Learn,magic,TINIFile,object
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Delphi Stats

 Code: 209,911. lines
 Jobs: 14. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Delphi.
Record From Microphone to Wave File
By Una-Rat on 11/25


List Manager
By james hill on 11/17


Click here to see a screenshot of this code!Skin app
By Jonathan Curry on 11/15

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

TINIFile And You - Using INI Files in Delphi

Print
Email
 

Submitted on: 1/19/2002 1:42:31 AM
By: Charles Chadwick  
Level: Beginner
User Rating: By 13 Users
Compatibility:Delphi 5, Delphi 4

Users have accessed this article 10007 times.
 

(About the author)
 
     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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:Kush Desai
Well written. Keep up the good work.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/18/2002 4:13:03 AM:Bree
very well done, yes please :) keep up the good work.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/5/2002 12:44:05 PM:Youdao
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/28/2002 8:29:30 PM:Charles Chadwick
I doubt you did, but that would be pretty cool =).
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/9/2002 3:35:01 PM:
Nice simple article, thanks. Perhaps you (unnamed) thinking of Tomes of Delphi, co-Author Charles Calvert :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/14/2003 3:29:16 PM:Wayne & 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.co m Delphi Fire Security Resources http://delphi.carrz-fox-fire. com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/4/2003 4:48:10 PM:
Excellent code. Explained well. Thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/2/2003 4:40:20 PM:Scott 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/30/2003 7:32:42 PM:
Wow, this is fantastic and is definantly superior to the visual basic methods. Long live delphi :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/5/2003 1:29:55 PM:slushey
Three words: Oh my god. R0x0r!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Delphi Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.