Jk Robbin
Jk Robbin

Reputation: 65

Delphi7 TIniFile Usage

Am trying to create an ini file to save the an application configuration. the saving part works perfect with the input from the edit boxes 1,2,3 etc here is the ini sample

[1server]
SSHHost=ssh.com
SSHPort=443
Username=user
Password=123
[2server]
SSHHost=ssh.com
SSHPort=443
Username=user
Password=123
[ProxySettings]
Proxy=127.0.0.1
Port=8080
Type=http

how do i make the app read the saved ini setting on launching it and is it possible to hide or encrypt user saved password?

Upvotes: 0

Views: 1548

Answers (1)

Ken White
Ken White

Reputation: 125689

Simply read the Ini file in your main form's FormCreate event.

procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(YourIniFileName);
  try
    // The final parameter to all of the `ReadXx` functions is a default
    // value to use if the value doesn't exist.
    Edit1.Text := Ini.ReadString('1server', 'SSHHost', 'No host found');
    Edit2.Text := Ini.ReadString('1server', 'SSHPort', 'No port found');
    // Repeat, using ReadString, ReadInteger, ReadBoolean, etc.
  finally
    Ini.Free;
  end;
end;

One word of caution: TIniFile has known issues with writing to network locations, so if there's any chance of that you should use TMemIniFile instead. TIniFile wraps the WinAPI INI support functions (ReadPrivateProfileString and others), while TMemIniFile is written totally in Delphi code and doesn't suffer from the same problems. They're syntax-compatible and in the same unit, so it's a simple matter of changing TIniFile to TMemIniFile in your variable declaration and the line that creates the Ini:

var
  Ini: TMemIniFile;
begin
  Ini := TMemIniFile.Create(YourIniFileName);
  ...
end;

As far as encrypting the password, you can use any encryption algorithm you want, as long as the encrypted value can be converted to a textual representation. (Ini files don't handle binary values.) The choice of algorithm is up to you based on the level of security you're attempting to achieve.

Upvotes: 3

Related Questions