⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 读写ini文件的Delphi程序
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,IniFiles, StdCtrls, Buttons; //(增加一个类型)

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Edit1: TEdit;
    Label1: TLabel;
    BitBtn2: TBitBtn;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
    myinifile:Tinifile ; //然后,就可以对变量myinifile进行创建、打开、读取、写入等操作了。
    ServerIP  :string;
    ServerName :string;
    DataName  :string;
    Password  :string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);   //读取INI文件
Var
  Dir :String;
begin
  Getdir(0,dir);
  myinifile :=TIniFile.Create(Dir+'\serverlianjie.ini');  //打开这个文INI文件
   try
    ServerIP :=myinifile.ReadString('SrvInfo','ServerIP','192.168.1.105');   //SrvIni为ini内容的抬头
    ServerName :=myinifile.ReadString('SrvInfo','ServerName','Soft');
    DataName :=myinifile.ReadString('SrvInfo','DataName','GDZC');
    Password :=myinifile.ReadString('SrvInfo','Password','123');
    Edit1.text:=Trim(ServerIP);
    Edit2.text:=Trim(ServerName);
    Edit3.text:=Trim(DataName);
    Edit4.text:=Trim(Password);
    finally
    FreeAndNil(myinifile);   //释放这个 INI文件
  end;


end;


procedure TForm1.BitBtn2Click(Sender: TObject);  //读取INI文件
var
Filename :string;
myinifile:Tinifile ;
begin
   Filename:=ExtractFilePath(Paramstr(0))+'serverlianjie.ini';   //打开程序目录下的INI文件
   myinifile:=Tinifile.Create(filename);  //把打开的INI文件作为一个变量来处理的
   try
    ServerIP :=myinifile.ReadString('SrvInfo','ServerIP','192.168.1.105');   //SrvIni为ini内容的抬头
    ServerName :=myinifile.ReadString('SrvInfo','ServerName','Soft');
    DataName :=myinifile.ReadString('SrvInfo','DataName','GDZC');
    Password :=myinifile.ReadString('SrvInfo','Password','123');
  //vi:=myinifile.Readinteger(’小节名’,’关键字’,缺省值);integer类型    Edit1.text:=Trim(ServerIP);
    Edit2.text:=Trim(ServerName);
    Edit3.text:=Trim(DataName);
    Edit4.text:=Trim(Password);
    finally
    FreeAndNil(myinifile);   //释放这个 INI文件
    end;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);     //写入INI文件,如果没有文件名会自动创建这个文件
var
Filename :string;
myinifile:Tinifile ;
TempServerIP,TempServerName,TempDataName,TempPassword :string;
begin
   Filename:=ExtractFilePath(Paramstr(0))+'serverlianjie.ini';   //打开程序目录下的INI文件
   myinifile:=Tinifile.Create(filename);  //把打开的INI文件作为一个变量来处理的
   try
    TempServerIP :=Trim(Edit1.text);
    TempServerName :=Trim(Edit2.text);
    TempDataName :=Trim(Edit3.text);
    TempPassword :=Trim(Edit4.text);
    myinifile.writestring('SrvInfo','ServerIP',' '''+TempServerIP+''' ');   //SrvIni为ini内容的抬头
    myinifile.writestring('SrvInfo','ServerName',' '''+TempServerName+''' ');  //变量的表示方法'''++'''外面的两个点点''为字符的的点
    myinifile.writestring('SrvInfo','DataName',' '''+TempDataName+''' ');
    myinifile.writestring('SrvInfo','Password',' '''+TempPassword+''' ');
    Application.MessageBox('修改完毕!','OK',MB_OK+MB_ICONWarning);
    except    //Try EXCEPT 为处理异常任务的语句
     Application.MessageBox('失败!','请重新修改',MB_OK+MB_ICONWarning);
   finally
    FreeAndNil(myinifile);   //释放这个 INI文件
   end;
//  myinifile.DeleteKey('小节名','关键字');    //可以删除关键字
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -