Solved

problem with ReadSectionValues

Posted on 1999-01-16
14
1,463 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

22 Experts available now in Live!

Get 1:1 Help Now