Here is a complete introduction to TIniFile objects: Using Delphi built-in functions to read and write INI files
After reading the above introduction, we started to read and write the INI file.
First, prepare an INI file in the same directory as the program. The file content is as follows:
[System]
Server = 127.0.0.1
DataBaseName = pubs
UserName = sa
Password =
Then, draw a form, add four Edit controls and a Button control, and use the default name.
Finally, write the code:
Implementation
Uses IniFiles;
Var
IFile: TiniFile;
{$ R *. dfm}
Procedure TForm1.FormCreate (Sender: TObject); {value of INI file read when the form is loaded}
Begin
IFile: = TIniFile. Create (ExtractFilePath (Application. ExeName) + 'data. ini ');
Try
Edit1.Text: = iFile. ReadString ('system', 'server ','');
Edit2.Text: = iFile. ReadString ('system', 'databasename ','');
Edit3.Text: = iFile. ReadString ('system', 'username ','');
Edit4.Text: = iFile. ReadString ('system', 'Password ','');
Finally
IFile. Free;
End;
End;
Procedure TForm1.Button1Click (Sender: TObject); {the value of the INI file written when you click the button}
Begin
IFile: = TIniFile. Create (ExtractFilePath (Application. ExeName) + 'data/data. ini ');
Try
IFile. WriteString ('system', 'server', Edit1.Text );
IFile. WriteString ('system', 'databasename ', Edit2.Text );
IFile. WriteString ('system', 'username', Edit3.Text );
IFile. WriteString ('system', 'Password', Edit4.Text );
Finally
IFile. Free;
End;
End;
Ini. Free;
End;
Var
Iserver, idatabasename, iUserName, iPassword: string;
IPort, itime: string;
Begin
IFile: = TIniFile. Create (ExtractFilePath (Application. ExeName) + 'pdaservice. ini ');
Try
Iserver: = iFile. ReadString ('data', 'server ','');
Idatabasename: = iFile. ReadString ('data', 'databasename ','');
IUserName: = iFile. ReadString ('data', 'username ','');
IPassword: = iFile. ReadString ('data', 'Password ','');
IPort: = iFile. ReadString ('Port', 'serviceport ','');
Itime: = iFile. ReadString ('time', 'interval ','');