Difference between revisions of "Using INI Files"
m (→Example: Grammar)
|Line 20:||Line 20:|
Now move on the the code..
Revision as of 09:39, 5 August 2011
INI files can be used to save basic user settings easily. With the INIfiles unit and the TINIFile class you can easily work with existing INI files. This unit is found in the FCL.
INI Files use brackets to create and mark Sections, which contain keys and key values. A Key and its corresponding Value are separated with an equals sign (Key=Value). Section names are put inside square brackets ([Section]). INI Files are used less than XML files for string storage, because INI files don't handle large strings very well.
The first thing we shall do is a simple console Application. First thing you should do is create an INI file and call it DB.ini and put it in your C:\ drive. Edit it to so it contains a section called INIDB and the following keys and values:
Author=Adam Pass= DBFile=C:\Money.dat
Now let's move on the the code.. <Delphi> Program Project1;
INI:TINIFile; Author,Pass,DBFile:String; PassEnter:String;
INI := TINIFile.Create('C:\DB.ini'); Author := INI.ReadString('INIDB','Author',); Pass := INI.ReadString('INIDB','Pass',); DBFile := INI.ReadString('INIDB','DBFile',); if Pass <> then begin Writeln('Password Required'); Repeat Readln(PassEnter); if not PassEnter = Pass then Writeln('Wrong Password'); until(PassEnter = Pass); Writeln('Correct Password'); end; Writeln('Author : '+Author); Writeln('File : '+DBFile); Writeln('Password : '+Pass); Readln; Ini.Free; // After we used ini file, we must call the Free method of object
Objects to know
In the TINIFile class there are many different Propertys, procedures and functions to be used.
CaseSensitive - This property allows you to say if the keys and sections are case sensitive or not.. by default they aren't..
ReadString - Has 3 constant statements. the first one is the section to search in. The second one is the key to look for. The third one is a default string incase the key and\or section searched for is not found.
WriteString has three constant statements too... The first is the section. The second is the key and the last is the Value. If the key and section exist already the Key will be over written with the new value..
ReadSections - Will allow you to to take the sections from the INI file and put them in a TStrings class(Or TStringList with the AS code)
DeleteKey - Remove an existing Key from a specific section
EraseSection Remove a section and all its data
There are more procedures and functions but this is basic..
Here:  you can learn all about INI Files to.. Please, if you can put up more information about INI files in Pascal. Modify At Will