• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1525
  • Last Modified:

problem with ReadSectionValues

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
mathes
Asked:
mathes
  • 5
  • 4
  • 3
  • +1
1 Solution
 
viktornetCommented:
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
 
kretzschmarCommented:
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
 
kretzschmarCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
viktornetCommented:
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
 
viktornetCommented:
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
 
mathesAuthor Commented:
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
 
bruintjeCommented:
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
 
viktornetCommented:
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
 
viktornetCommented:
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
 
bruintjeCommented:
OK viktor that should do it!
:P)
0
 
bruintjeCommented:
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
 
mathesAuthor Commented:
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
 
bruintjeCommented:
Good Morning !:O) and you're right!
0
 
kretzschmarCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now