发新话题
打印

[语言基础/算法] delphi读写ini文件

delphi读写ini文件

下面是一个完整的例子,它介绍了一个INI文件的基本使用方法,让初入门的读者对它有一
个简单的认识。而且这个例程的结构性还算不是很糟,应该是很容易理解的。

例子中先是用 USE 引用了 INI 的代码单元.

我们在窗体上放了两上按钮,按钮1是用来往INI文件里写信息的,按钮2则从中读出来。

从中可以看出,INI文件在读写之前要先建一个变量将它联系起来,再进行读写。在读写完
之后及时进行释放,以使信息可以准确的写进INI文件中。例如:
  FINI.WriteString('MainGrou', 'SubStr', 'This is a string.');
这一句,'MainGrou'是它的主键,也就是在INI文件中用中括号括着的。'SubStr'是子键,
它隶属于一个指定的主键,在INI文件中显示成一个在等号前面的字符串。
'This is a string.'是这个子键的值。对于一个指定的INI文件,只可有一个指定名称的主
键,而每一个不同的主键则可以用同名的子键保存各自的信息而不互相干扰。

程序中,我们用 Try..Finally 语句来确保无论任何情况下它都能够得到释放,这的确
是个好主意,其它一些应该释放的东西也应该这么做的。

按钮2中我们读出了INI文件的内容。我们注意到Read...()函数的第三个参数,那是它的默
认值,也就是如果在INI文件中并没有此子键,就用这个默认值作为返回值。我们读出 V3
后进行判断,如果是TRUE则读一个字符串进行显示,否则读两个整数并取和显示出来。

因为经常引用到 FINI 这个变量,在按钮2中我们用 With 使整个程序看起来更加直接,结
构更加明确,但要注意,应用 With 应该适当,不要太滥,如果这样只会让程序更加难看,
有时还会引起引用错乱。
==========================================
复制内容到剪贴板
代码:
unit Unit1;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
   Dialogs, StdCtrls, INIFiles;

type
  TForm1 = class(TForm)
           Button1: TButton;
           Button2: TButton;
           procedure Button1Click(Sender: TObject);
           procedure Button2Click(Sender: TObject);
         private
           { Private declarations }
         public
           { Public declarations }
         end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
Var
  FINI : TINIFile;
begin
  FINI := TINIFile.Create('C:\Test.INI');
  Try
    FINI.WriteString('MainGrou', 'SubStr', 'This is a string.');
    FINI.WriteInteger('MainGrou', 'Int1', 88);
    FINI.WriteInteger('MainGrou', 'Int2', 345);
    FINI.WriteBool('MainGrou', 'Bool', True);
  Finally
    FINI.Destroy;
  End;
end;

procedure TForm1.Button2Click(Sender: TObject);
Var
  FINI : TINIFile;
  V1, V2 : Integer;
  V3 : Boolean;
  V4 : String;
begin
  FINI := TINIFile.Create('C:\Test.INI');
  With FINI do
    Try
      V3 := fini.ReadBool('MainGrou', 'Bool', True);
      If V3 then
         begin
              V4 := fini.ReadString('MainGrou', 'SubStr', 'Read Error.');
              ShowMessage(V4);
         end
      else
         begin
              V1 := fini.ReadInteger('MainGrou', 'Int1', 0);
              V2 := fini.ReadInteger('MainGrou', 'Int2', 0);
              ShowMessage(IntToStr(V1+V2));
         end;
    Finally
      Destroy;
    End;
end;
end.
妖城欢迎您!

TOP

这里还有一个实例代码

首先准备一个ini文件,位置和程序在同一个目录下,文件内容如下:
[System]
Server=127.0.0.1
DataBaseName=pubs
UserName=sa
Password=

然后开始画窗体,添加四个Edit控件,和一个Button控件,使用默认的名称

最后写代码:
implementation
uses IniFiles;
var
  iFile :TiniFile;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);{窗体加载时读取ini文件的值}
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);{点击按钮时写入ini文件的值}
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;
妖城欢迎您!

TOP

  在Windows中利用.INI文件做程序有关数据的存储工作是很常见的,其中涉及了读和写.INI文件问题,下面就介绍几种不同的方法给大家参考:

  从.INI文件中获取字符串

  var
strResult:pchar;
begin
GetPrivateProfileString(
windows, // []中标题的名字
NullPort, // =号前的名字
NIL, // 如果没有找到字符串时,返回的默认值
strResult, //存放取得字符
100, //取得字符的允许最大长度
c:\forwin95\win.ini // 调用的文件名
);
edit1.text:=strResult; //显示取得字符串

  从.INI文件中获取整数

  edit1.text:=inttostr(GetPrivateProfileInt(
intl, // []中标题的名字
iCountry, // =号前的名字
0,// 如果没有找到整数时,返回的默认值
c:\forwin95\win.ini // 调用的文件名
));

  向.INI文件写入字符串

  WritePrivateProfileString(
windows, // []中标题的名字
load, // 要写入“=”号前的字符串
accca, //要写入的数据
c:\forwin95\win.ini // 调用的文件名
);

  向.INI文件写入整数

  WritePrivateProfileSection(
windows, // []中标题的名字
read=100, // 要写入的数据
c:\forwin95\win.ini // 调用的文件名
);

  上面的方法是调用API函数,下面介绍另一种不用API,而是使用TIniFile从.INI文件中获取字符的方法

  从.INI文件中读字符

  var MyIni: TIniFile;
begin
MyIni := TIniFile.Create(WIN.INI);//调用的文件名
edit1.text:=MyIni.ReadString(Desktop, Wallpaper, );//取得字符
end;

  向.INI文件中写入字符

  var MyIni: TIniFile;
begin
MyIni := TIniFile.Create(WIN.INI);//调用的文件名
DelphiIni.WriteString(Desktop, Wallpaper, c:\a.jpg);//写入字符
end;

INI文件函数,也提供给大家参考: function GetINIfile(lpAppNameL,lpKeyName,lpDefault:string;
lpsize:integer;lpFileName:string):string;
{读取ini文件函数}
var f:textfile;
sn:string;
begin
assignfile(f,lpFileName);
reset(f);
repeat
readln(f,sn);
if sn=[+lpAppNameL+] then
begin
readln(f,sn);
while(copy(sn,1,1)<>[)or(not(eof(f)))do
begin
if copy(sn,1,pos(=,sn)-1)=lpKeyName then
begin
GetINIfile:=copy(sn,pos(=,sn)+1,lpsize);
exit;
end;
readln(f,sn);
end;
end
else GetINIfile:=lpDefault;
until eof(f);
closefile(f);
end;
{------------}
  调用方法是:


  var Timeout:String;
begin
Timeout:=GetINIfile(MailSetup,Timeout,0,5,prgpath+\CNMSet.ini);
end;
妖城欢迎您!

TOP

发新话题