0
votes

Im stuck and asking for your help te get a solution for reading my ini file back and placing that in my memo1 form with a button.

This is in my text file:

[Filename]

Work Time=03-10-2018 15:11

Here is some of the code im working with.

var
  aWorkTime: string;

procedure TForm1.button2(Sender: TObject):
begin
  Memo1.Lines.Clear;

  IniFile := TIniFile.Create(GetCurrentDir+'\Filename.ini');
try
  aWorkTime := IniFile.ReadString('Filename', 'Work Time', <'none'>);
finally
  IniFile.Free;
end;
end

I hope that this is enough information if not please tell me what you are missing from me

1
FWIW, GetCurrentDir probably isn't what you need. That can change, and is not necessarily the directory of your application.Jerry Dodge
Ah yes File needs to be work time yes my bad typed out the wrong name there. And if i remove {GetCurrentDir} I get a error the error Not enough actual parameters.Noisse
If you want to read the entire ini file into your memo, then use something like Memo1.Lines.LoadFromFile(FullIniFileName);.Rudy Velthuis
To get the file name, use ExtractFilePath(Application.ExeName) + 'FileName.ini'. Do not use GetCurrentDir.Rudy Velthuis

1 Answers

2
votes

Your use of GetCurrentDir is problematic. The current dir may change and doesn't have to be the same directory as where your .exe file resides. Rather use ExtractFilePath(Application.ExeName)

Also, instead of reading the items one by one, to read the entire .ini file into your memo, do something like:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.LoadFromFile(ExtractFilePath(Application.ExeName) + 'FileName.ini');
end;

If you only need the work time, then your code is almost there:

var
  aWorkTime: string;
  IniFile: TIniFile;
begin  
  Memo1.Lines.Clear;
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'FileName.ini');
  try
    aWorkTime := IniFile.ReadString('Filename', 'Work Time', '<none>');
    Memo1.Lines.Add('Work Time=' + aWorkTime);
  finally
    IniFile.Free;
  end;
end;