Question

I am looking for a way to create a configuration file that does not involve IniFile.

I have 2 editboxes in a Delphi application, the first is named username1 in editbox1, and the second named passwords in editbox2.

I want to create a configuration file as shown below in the figure as an example configuration:

superuser is the username1 that is entered to editbox1

passw123 is the password entry to editbox2

Here I do not want to involve IniFile, the point I want to make a configuration file as shown below with txt with notepad application that only make a config file that lined

superuser
passw123
Was it helpful?

Solution 3

It is a little difficult to understand what exactly you are asking, but if you are asking how to save and load a configuration containing a user name and a password to a simple text file, you can use this code:

TYPE
  TConfiguration = RECORD
                     UserName,Password : STRING
                   END;

FUNCTION ReadConfig(CONST FileName : STRING) : TConfiguration;
  VAR
    TXT : TextFile;

  BEGIN
    AssignFile(TXT,FileName); RESET(TXT);
    TRY
      READLN(TXT,Result.UserName);
      READLN(TXT,Result.Password)
    FINALLY
      CloseFile(TXT)
    END
  END;

PROCEDURE WriteConfig(CONST FileName : STRING ; CONST Config : TConfiguration);
  VAR
    TXT : TextFile;

  BEGIN
    AssignFile(TXT,FileName); REWRITE(TXT);
    TRY
      WRITELN(TXT,Config.UserName);
      WRITELN(TXT,Config.Password)
    FINALLY
      CloseFile(TXT)
    END
  END;

Use it as this when saving:

VAR Config : TConfiguration;
...
Config.UserName:=editbox1.Text;
Config.Password:=editbox2.Text;
WriteConfig(FileName,Config);

and like this to reload:

VAR Config : TConfiguraion;
...
Config:=ReadConfiguration(FileName);
editbox1.Text:=Config.UserName;
editbox2.Text:=Config.Password;

If this is not what you want, you'll have to elaborate a bit...

OTHER TIPS

When you have a file containing configuration info on separate lines:

superuser
password123

You can use a TStringList to read those:

procedure GetConfig(const AFileName: string; out AValue1, AValue2: string);
var
  list : TStringList;
begin
  list := TStringList.Create;
  try
    list.LoadFromFile(AFileName);
    if list.Count<2 then begin
      // ERROR
    end;
    AValue1 := list[0];
    AValue2 := list[1];
  finally
    FreeAndNil(list);
  end;
end;

I assume you can figure out the write part yourself.

You can add any error handling you like.

You are mixing the layers in your mind and therefore in your application.

You should think more abstract. The UI does not need to know where and how the information is stored, the UI only needs to know how to get the information.

TMyConfigData = class abstract
protected
  function GetUsername : string; virtual; abstract;
  function GetPassword : string; virtual; abstract;
public
  property Username : string read GetUsername;
  property Password : string read GetPassword;
end;

The concrete class (text-file based) looks like this

TMyTextConfigData = class( TMyConfigData )
private
  FFilename : string;
  FData : TStringList;
protected
  function GetUsername : string; override;
  function GetPassword : string; override;
public
  constructor Create( const Filename : string );
  destructor Destroy; override;
end;

constructor TMyTextConfigData.Create( const Filename : string );
begin
  inherited Create;
  FFilename := Filename;
  FData := TStringList.Create;
  FData.ReadFromFile( FFilename );
end;

destructor Destroy;
begin
  FData.Free;
  inherited;
end;

function TMyTextConfigFile.GetUsername : string;
begin
  Result := FData[0]; // first line
end;

function TMyTextConfigFile.GetPassword : string;
begin
  Result := FData[1]; // second line
end;

Once you decide to use INI-File or XML-File or registry or even a database you only build a new concrete class for that. Your application (UI) will only know the instance of TMyConfigData and does not need to be changed.

I see it's a password in there. Probably by ini files you meant visible text. You could hide your configuration somewhere in windows registry using TRegistry, or a ClientDataSet table, who's content isn't easy to read using notepad.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top