Solved

problem with ReadSectionValues

Posted on 1999-01-16
14
1,470 Views
Last Modified: 2012-06-27
Hi experts,

I have problems to read the values from a *.ini file.

I created this myprog.ini file:

[Path of my program]
program-path=C:\MYPATH


With the help of the following routine, I would like to copy the string

C:\MYPATH

to edit1.text;

procedure TForm1.Button1Click(Sender: TObject);
var
  winposhIni: TIniFile;
  i, position: integer;
  temp: string;
  sl: tstringlist;
begin
  if fileexists('myprogram.ini')
    then begin
    myprogIni := TIniFile.Create('myprogram.ini');
    sl := tstringlist.create;
    myprogIni.ReadSectionValues('Path of my program', sl);
    for i := 0 to sl.count - 1 do
      temp := sl.strings[i];
    position := pos('=', temp);
    delete(temp, 1, position);
    edit1.text := temp;
    myprogIni.Free;
    sl.free;
  end;
end;

However when I run my demo program, the variables "temp" and "sl"
are always empty, although my demo program and myprog.ini are both
in the same direcory and do of course both exist. But edit1.text is always
blank and I can never see C:\MYPATH in this field.

Can you please tell me, what I am doing wrong here?

With kind regards

Mathes
0
Comment
Question by:mathes
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 10 total points
ID: 1362162
I think your new code should look like this...

procedure TForm1.Button1Click(Sender: TObject);
   var
     winposhIni: TIniFile;
     i, position: integer;
     temp: string;
     sl: tstringlist;
   begin
     if fileexists('myprogram.ini')  then begin
       myprogIni := TIniFile.Create('myprogram.ini');
       sl := tstringlist.create;
       myprogIni.ReadSectionValues('Path of my program', sl);
       for i := 0 to sl.count - 1 do begin
         temp := sl.strings[i];
         position := pos('=', temp);
         delete(temp, 1, position);
         edit1.text := temp;
      end;
       myprogIni.Free;
       sl.free;
     end;
   end;

Try it and tell me how it goes... It would change all the values in the editbox very fast so you might want to save your new values in a string list or something...

-Viktor
--Ivanov
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1362163
Hi Mathes,

    myprogIni.ReadSectionValues('Path of my program', sl);

reads only the Names of the section
in this case only
program-path

you can use this(TStringlist not used by this)

  Edit1.Text := MyIniFile.ReadString'Path of my program','program-path','NA');

Meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1362164
hi Mathes,
instead of this in my comment

myprogIni.ReadSectionValues('Path of my program', sl);

reads only the Names of the section
in this case only
program-path

I Meant this

myprogIni.ReadSectionValues('Path of my program', sl);

reads only the Values of the section
in this case only
C:\MYPATH

Meikl

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Expert Comment

by:viktornet
ID: 1362165
i think that's ahwt mathes wants and he doesn't have to delete all the stuff.... so he could just do this...

Put a listbox on your form...

procedure TForm1.Button1Click(Sender: TObject);
      var
        winposhIni: TIniFile;
      begin
        if fileexists('myprogram.ini')  then begin
          myprogIni := TIniFile.Create('myprogram.ini');
          myprogIni.ReadSectionValues('Path of my program', ListBoxName);
          myprogIni.Free;
        end;
      end;

That's all... Let me know how it goes....

btw- If you want to read only one value, you should do as kretchmar proposed....

MyPath := MyIniFile.ReadString('Path of my program','program-path','');

That's all....

-Viktor
--Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362166
A little correction....

procedure TForm1.Button1Click(Sender: TObject);
           var
             winposhIni: TIniFile;
           begin
             if fileexists('myprogram.ini')  then begin
               myprogIni := TIniFile.Create('myprogram.ini');
               myprogIni.ReadSectionValues('Path of my program', ListBoxName.Items);
               myprogIni.Free;
             end;
           end;
0
 

Author Comment

by:mathes
ID: 1362167
Hi experts,

I meanwhile tried out both of your interesting suggestions.

My code meanwhile is:

unit readini;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Edit1: TEdit;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen}
  public
    { Public-Deklarationen}
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  myprogIni: TIniFile;
  mypath: string;
begin
  if fileexists('myprogram.ini') then begin
    myprogIni := TIniFile.Create('myprogram.ini');
    MyPath := MyprogIni.ReadString('Path of my program', 'program-path', '');
    edit1.text := mypath;
    myprogIni.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  myprogIni: TIniFile;
begin
  if fileexists('myprogram.ini') then begin
    myprogIni := TIniFile.Create('myprogram.ini');
    //myprogIni.ReadSectionValues('Path of my program', ListBox1); { error !!!! }
    myprogIni.Free;
  end;
end;

end.


the resul of my test is as follows:

button1: the listbox remains empty during runtime, I can't see any content from myprog.ini in this section
of my form.

button2: this procedure contains an error.

In line

myprogIni.ReadSectionValues('Path of my program', ListBox1);

Delphi says:

incompatible data types Tstrings and TStringlist.

What can I do now?

With kind regards

Mathes

0
 
LVL 44

Expert Comment

by:bruintje
ID: 1362168
Well it took me some time to figure it out, but I think I got the solution! :O))))

After trying everything, from setting the results in messages and reading the inifile in a memo, think you should set your directory pointing to the directory where your ini resides. In my case it was 'c:\myprogram.ini' and this even worked with your original code you posted above.

Hope this helps.
brUINTje.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362169
OK, I didn't tested any of the code I gave you so far, but I finally test it and here is one that works perfectly.... I've got an EditBox, two buttons, and one listbox.... here is the whole unit....

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses IniFiles;

const
      FileName = 'C:\windows\desktop\MyProgram.Ini';

procedure TForm1.Button1Click(Sender: TObject);
var
       myprogIni: TIniFile;
       mypath: string;
     begin
       if fileexists(FileName) then begin
         myprogIni := TIniFile.Create(FileName);
         MyPath := MyprogIni.ReadString('Path of my program', 'program-path', '');
         edit1.text := mypath;
         myprogIni.Free;
       end;
     end;
procedure TForm1.Button2Click(Sender: TObject);
var
       myprogIni: TIniFile;
       str : string;
       i : integer;
     begin
       if fileexists(FileName) then begin
         myprogIni := TIniFile.Create(FileName);
         ListBox1.Items.BeginUpdate;
         myprogIni.ReadSectionValues('Path of my program', ListBox1.Items);
         for i := 0 to ListBox1.Items.Count - 1 do begin
               str := ListBox1.Items[i];
               ListBox1.Items[i] := Copy(str, Pos('=', str) + 1, Length(str));
         end;
         ListBox1.Items.EndUpdate;
         myprogIni.Free;
       end;
     end;
end.

Hope this helps...

-Viktor
--Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362170
bruntje, it works with his first code because there is on;y one item in there.... try putting in two or more, and you'll see that you'll always get the last filepath, and not all of them.....

-Viktor
--Ivanov
0
 
LVL 44

Expert Comment

by:bruintje
ID: 1362171
OK viktor that should do it!
:P)
0
 
LVL 44

Expert Comment

by:bruintje
ID: 1362172
you got that 12:06 comment posted before my 12:07 ? probably my computer is somewhat running into the future. But what the hack :O), if you want I can post some code with two listboxes, and a memo. The memo is for creating your ini and the listboxes the first will display the all sections in the ini, and the second will show the values in the section.

:O)))
Always a pleasure
0
 

Author Comment

by:mathes
ID: 1362173
Hi experts,

thank you all for your help. I am convinced we all had a benefit from this discussion and learned
something new.
The tested code of victor really works bugfree, so I think he deserved the points.

With kind regards

Mathes


0
 
LVL 44

Expert Comment

by:bruintje
ID: 1362174
Good Morning !:O) and you're right!
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1362175
Hi Mathes, Hi experts,

i take a mistake for ReadSectionValues with ReadSection
of Course in ReadSectionValues come the whole line(s)
xx=yy

to get then only the yy-part, there can also used this fragment

  For i := 0 to ListBox1.Items.Count - 1 do
    ListBox1.Items[I] := ListBox1.Items.Values[ListBox1.Items.Names[I]];


meikl
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now