Search code examples
delphidelphi-7pascalini

how to create a configuration file with not involve IniFile?


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

Solution

  • 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...