Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use TIniFile in simple steps please 1

Status
Not open for further replies.

doctorjellybean

Programmer
May 5, 2003
145
This might be simple for some, but I really have a problem in understanding exactly how it works. I've tried Google, and the majority of them appears to deal with form display settings, etc.

This is my scenario: The user has to make some selections in the Options section, and those selections need to be stored somewhere. At the moment it is stored in a text file, and the info in that is loaded into a listbox when the program starts up. The selections are e.g. default folder, enable/disable hints, use built-in or default browser.

Basically I would like to know how to write the settings in a INI File, and to read from it.

I do apologize if I sound stupid lol [sad]
 
Use TIniFile.Create to open or create the INI file.

Use any of the Write or Read methods to modify or read a particular value.

INI files are organised into sections which you specify in each Write or Read method.

That's all there is to it. Once you have these basics working, you can investigate using some of the other helper methods available.
 
its all in the help file.

This example reads the Transfer section of the DELPHI32.INI file into a memo and changes one of the strings in the INI file when Button1 is clicked. When Button2 is clicked, the DELPHI32.INI file is restored to its initial state, using the values stored in the memo.
Before you run this example, you must add the IniFiles unit to the uses clause of your unit.

Warning: Do not click button2 before you have clicked button1!

procedure TForm1.Button1Click(Sender: TObject);

var
DelphiIni: TIniFile;
begin
DelphiIni := TIniFile.Create('c:\windows\delphi32.ini');
Memo1.Clear;
DelphiIni.ReadSectionValues('Transfer', Memo1.Lines);
if Memo1.Lines.Values['Title1'] <> 'Picture Painter' then
DelphiIni.WriteString('Transfer', 'Title1', 'Picture Painter');
DelphiIni.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);

var
DelphiIni: TIniFile;
begin

DelphiIni := TIniFile.Create('c:\windows\delphi32.ini');

{ if the entry wasn’t there before, delete it now }
if Memo1.Lines.Values['Title1'] = '' then
DelphiIni.DeleteKey('Transfer', 'Title1')
{ otherwise, restore the old value }
else
DelphiIni.WriteString('Transfer', 'Title1', Memo1.Lines.Values['Title1']);
DelphiIni.Free;
end;

Aaron
 
Thanks aaronjme.

I'm afraid that the D2007 help file is not very, well, helpful. I much prefer the help files of the earlier versions.
 

I find the same.
I'm sure when I last used Delphi (v3 !) the help was really helpful.

I struggle to get much help from help in 2007.


Steve (Delphi 2007 & XP)
 
i agree. i was so dissapointed with all the bugs and poor help in D2007 that i reverted back to D7.

Aaron
 
I'll just add my voice to this: The general user experience of TD2006 was just so bad. Then I found several features that I was used to seeing and deemed essential either missing (command-line compiler?) or so locked down (components creation), not to mention it's genuine slowness and extreme clutter, that it became useless. Then the EULA is pretty restrictive (as far as I can see) on what one can or can not do.

So I scrapped the thing. Hopefully Borland, Inprise, CodeGear, or whatever they're calling themselves today can get their act together.

----------
Those who work hard are rewarded with more work and remembered come time to downsize. Those who hardly work are given a paycheck and ignored completely.
 
Glenn9999 - out of curiosity, what restrictions did you see in the EULA that made it restrictive to you?
 
I use D2006 since it's release and I am very pleased with it! Ok, it took 3 updates + a bunch of hotfixes to get the damn thing stable but after using all the new features, it's hard to return to older delphi versions...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 

a question on the EULA..

Am I likely to be OK if I make my app (when finished!) available as freeware?


Steve (Delphi 2007 & XP)
 
Sorry to Hijack the post. :) You should be allowed to create any kind of application as long as you aren't creating a new IDE with your product. That's what the EULA for MS Access basically said - you can install the MDAC but you can't use it to create a competitor for MS Access. I'm NOT a lawyer so take that as you will but I'm writing shareware and payware with my BDS2006 and don't intent to stop.

And I use TIniFile regularly.
 
LOL @ Hijack the post.
RE: Thread topic - INI flies are an excellent opportunity to start using properties. I've created a sample INI unit to demonstrate how this is done. The ini file has a single key "setup" with a single integer item "Tolerance".

In the private section of my form definition, I entered the following
Code:
property Tolerance: integer read GetTolerance write SetTolerance;
Delphi code completion does the rest. All that was left was to insert the registry read/write strings in the subsequent created functions.
Code:
unit IniUnit;

interface

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

type
  TSetupForm = class(TForm)
    Edit1: TEdit;
    SaveBtn: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure SaveBtnClick(Sender: TObject);
  private
    function GetTolerance: integer;
    procedure SetTolerance(const Value: integer);
  private
    { Private declarations }
    IniFile: TIniFile;
    AppPath: string;
    property Tolerance: integer read GetTolerance write SetTolerance;
  public
    { Public declarations }
  end;

var
  SetupForm: TSetupForm;

implementation

{$R *.dfm}

const
  IniFileName = 'BP.INI';

procedure TSetupForm.FormCreate(Sender: TObject);
begin
  AppPath:= ExtractFilePath(ParamStr(0));
  IniFile:= TiniFile.Create(AppPath + IniFileName);
  Edit1.Text:= IntToStr(Tolerance);
end;

procedure TSetupForm.FormDestroy(Sender: TObject);
begin
  IniFile.Free
end;

function TSetupForm.GetTolerance: integer;
begin
  Result:= IniFile.ReadInteger('Setup', 'Tolerance', 0);
end;

procedure TSetupForm.SetTolerance(const Value: integer);
begin
  IniFile.WriteInteger('Setup', 'Tolerance', value);
end;

procedure TSetupForm.SaveBtnClick(Sender: TObject);
begin
  Tolerance:= StrToInt(Edit1.Text);
  Close
end;

end.
This is the simplest step by step process that I can come up with.

HTH

Roo
Delphi Rules!
 
Can I partially hijack this thread... kinda.? :) If you mods want me to start a new thread, I can do that too, it's just that my question is similar to the OP... anyway:

I'm wanting to save different configurations of Delphi so I can load what I want when I want. I've used:

Delphi Distiller <
and

Delphi Package Selector <
but there are a few more tweaks that I want to easily configure, so it's time to roll my own, and I want to learn how.

I've figured out how to read the HKEY_CURRENT_USER\Software\Borland\BDS\5.0\Known IDE Packages registry key into a listview or a memo, then save the listview items into text file.

My question: How do I write the text file into the registry to replace the old HKEY_CURRENT_USER\Software\Borland\BDS\5.0\Known IDE Packages registry key with a new one using my saved text file?

I just recently heard about TRegIniFile, but don't have a clue how to use it. Can anyone give simple steps please. (At least this adheres to the OP question :))

doctorjellybean, I don't mean to hijack your thread, but since I saw yours and it being similar... thought it might fit. Let me know if you want me to make this a new one.
 
Reading and writing to the registry is almost identical to Reading and writing to an INI file. The main difference is that you have to grant yourself permission. (See Delphi help for TRegistry.Access)

In the INI example above, you may have noticed that I open the INI file on form create, then close it on form destroy. With the registry, it is safer to open and close it within the function itself. Below is an excerpt from one of my apps where I write a string value to the registry. The string value is passed to the procedure as an input parameter.
Code:
procedure TMakePDF.SetDocumentOutputName(PdfName: string);
var reg: TRegistry;
begin
  reg:= TRegistry.Create;
  try
    reg.Access:= KEY_ALL_ACCESS;
    reg.RootKey:= HKEY_CURRENT_USER;
    reg.OpenKey('\Software\NEEVIA\Neevia docuPrinter', false);
    Reg.WriteString('FileMask', PdfName);
  finally
    reg.Free;
  end;
end;
BTW to Dr.JB: There are no stupid questions, just stupid answers.

Ref: D7/XP

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top