whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

42 Visitors Online


 
...use INI files? (*.ini)
Autor: Simon Grossenbacher
Homepage: http://www.swissdelphicenter.ch
[ Print tip ]  

Tip Rating (133):  
     


{
  An INI file stores information in logical groupings, called “sections.”
  Within each section, actual data values are stored in named keys.
  
  [Section_Name]
  Key_Name1=Value1
  Key_Name2=Value2
  
}

uses
  
IniFiles;

// Write values to a INI file

procedure TForm1.Button1Click(Sender: TObject);
var
  
ini: TIniFile;
begin
  
// Create INI Object and open or create file test.ini
  
ini := TIniFile.Create('c:\MyIni.ini');
  try
    
// Write a string value to the INI file.
    
ini.WriteString('Section_Name', 'Key_Name', 'String Value');
    // Write a integer value to the INI file.
    
ini.WriteInteger('Section_Name', 'Key_Name', 2002);
    // Write a boolean value to the INI file.
    
ini.WriteBool('Section_Name', 'Key_Name', True);
  finally
    
ini.Free;
  end;
end;


// Read values from an INI file

procedure TForm1.Button2Click(Sender: TObject);
var
  
ini: TIniFile;
  res: string;
begin
  
// Create INI Object and open or create file test.ini
  
ini := TIniFile.Create('c:\MyIni.ini');
  try
    
res := ini.ReadString('Section_Name', 'Key_Name', 'default value');
    MessageDlg('Value of Section:  ' + res, mtInformation, [mbOK], 0);
  finally
    
ini.Free;
  end;
end;

// Read all sections

procedure TForm1.Button3Click(Sender: TObject);
var 
  
ini: TIniFile;
begin
  
ListBox1.Clear;
  ini := TIniFile.Create('MyIni.ini');
  try
    
ini.ReadSections(listBox1.Items);
  finally 
    
ini.Free; 
  end;
end;

// Read a section

procedure TForm1.Button4Click(Sender: TObject);
var 
  
ini: TIniFile;
begin
  
ini: = TIniFile.Create('WIN.INI');
  try
    
ini.ReadSection('Desktop', ListBox1.Items);
  finally
    
ini.Free;
  end;
end;


// Read section values

procedure TForm1.Button5Click(Sender: TObject);
var
  
ini: TIniFile;
begin
  
ini := TIniFile.Create('WIN.INI');
  try
    
ini.ReadSectionValues('Desktop', ListBox1.Items);
  finally 
    
ini.Free;  
  end;
end;

// Erase a section

procedure TForm1.Button6Click(Sender: TObject);
var 
  
ini: TIniFile;
begin
  
ini := TIniFile.Create('MyIni.ini');
  try
    
ini.EraseSection('My_Section');
  finally
    
ini.Free;
  end;
end;



 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners