?
Solved

problem with ReadSectionValues

Posted on 1999-01-16
14
Medium Priority
?
1,501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 40 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
Independent Software Vendors: 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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

771 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