ini 文件操作记要(1): 使用 TIniFile


unit
Unit1; interface uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls; type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end; var
  Form1: TForm1; implementation {$R *.dfm} uses IniFiles;  {uses 包含 TIniFile 的单元} var
  ini: TIniFile;
  path: string;        {ini 文件路径}
  Section,Key: string;  {分别表示 ini 文件的小节与关键字} {
  ini文件结构:
  ;注释
  [小节名]
  关键字=值   INI文件支持: string、integer、boolean、Date、Time、DateTime、Double 与二进制类型
  string 值没有引号
  boolean 的真假用 1、0 表示
} procedure TForm1.FormCreate(Sender: TObject);
begin
  path := ChangeFileExt(ParamStr(),'.ini');
  ini := TIniFile.Create(path);  {ini 对象建立需要文件路径参数, 如果缺少路径会默认Windows目录}
end; //写入 ini 文件:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Section := 'AAA';
  Key := 'AString';
  ini.WriteString(Section,Key,'AAA-String');   Key := 'AInteger';
  ini.WriteInteger(Section,Key,);   Key := 'ABoolean';
  ini.WriteBool(Section,Key,True);   Key := 'ADate';
  ini.WriteDate(Section,Key,Now);   Key := 'ATime';
  ini.WriteTime(Section,Key,Now);   Key := 'ADateTime';
  ini.WriteDateTime(Section,Key,Now);   Key := 'ADouble';
  ini.WriteFloat(Section,Key,Pi);   Section := 'BBB';
  Key := 'BString';
  ini.WriteString(Section,Key,'BBB-String');   Key := 'BInteger';
  ini.WriteInteger(Section,Key,);   Key := 'BBoolean';
  ini.WriteBool(Section,Key,True);   Key := 'BDate';
  ini.WriteDate(Section,Key,Now);   Key := 'BTime';
  ini.WriteTime(Section,Key,Now);   Key := 'BDateTime';
  ini.WriteDateTime(Section,Key,Now);   Key := 'BDouble';
  ini.WriteFloat(Section,Key,Pi);   Section := 'CCC';
  Key := 'CString';
  ini.WriteString(Section,Key,'CCC-String');   Key := 'CInteger';
  ini.WriteInteger(Section,Key,);   Key := 'CBoolean';
  ini.WriteBool(Section,Key,False);   Key := 'CDate';
  ini.WriteDate(Section,Key,Now);   Key := 'CTime';
  ini.WriteTime(Section,Key,Now);   Key := 'CDateTime';
  ini.WriteDateTime(Section,Key,Now);   Key := 'CDouble';
  ini.WriteFloat(Section,Key,Pi); {写入结果:
  [AAA]
  AString=AAA-String
  AInteger=111
  ABoolean=1
  ADate=2007-12-17
  ATime=22:06:23
  ADateTime=2007-12-17 22:06:23
  ADouble=3.14159265358979
  [BBB]
  BString=BBB-String
  BInteger=222
  BBoolean=1
  BDate=2007-12-17
  BTime=22:06:23
  BDateTime=2007-12-17 22:06:23
  BDouble=3.14159265358979
  [CCC]
  CString=CCC-String
  CInteger=333
  CBoolean=0
  CDate=2007-12-17
  CTime=22:06:23
  CDateTime=2007-12-17 22:06:23
  CDouble=3.14159265358979
}
end; //读取 ini 文件:
procedure TForm1.Button2Click(Sender: TObject);
var
  s: string;
  i: Integer;
  b: Boolean;
  f: Double;
  d: TDate;
  t: TTime;
  dt: TDateTime;
begin
  s := ini.ReadString('BBB','BString','');  {最后一个参数是默认值}
  i := ini.ReadInteger('BBB','BInteger',);
  b := ini.ReadBool('BBB','BBoolean',False);
  f := ini.ReadFloat('BBB','BDouble',);
  d := ini.ReadDate('BBB','BDate',Now);
  t := ini.ReadTime('BBB','BTime',Now);
  dt := ini.ReadDateTime('BBB','BDateTime',Now);   ShowMessage(s);                {BBB-String}
  ShowMessage(IntToStr(i));      {222}
  ShowMessage(BoolToStr(b));      {-1(True)}
  ShowMessage(FloatToStr(f));    {3.14159265358979}
  ShowMessage(DateToStr(d));      {2007-12-17}
  ShowMessage(TimeToStr(t));      {22:06:23}
  ShowMessage(DateTimeToStr(dt)); {2007-12-17 22:06:23}
end; //读入所有小节名到 TStrings:
procedure TForm1.Button3Click(Sender: TObject);
var
  List: TStrings;
begin
  List := TStringList.Create;
  ini.ReadSections(List);
  ShowMessage(List.Text);
  {
    AAA
    BBB
    CCC
  }
  List.Free;
end; //读入指定小节的所有关键字到 TStrings:
procedure TForm1.Button4Click(Sender: TObject);
var
  List: TStrings;
begin
  List := TStringList.Create;
  ini.ReadSection('AAA',List);
  ShowMessage(List.Text);
  {
    AString
    AInteger
    ABoolean
    ADate
    ATime
    ADateTime
    ADouble
  }
  List.Free;
end; //读入指定小节的所有关键字与值到 TStrings:
procedure TForm1.Button5Click(Sender: TObject);
var
  List: TStrings;
begin
  List := TStringList.Create;
  ini.ReadSectionValues('BBB',List);
  ShowMessage(List.Text);
  {
    BString=BBB-String
    BInteger=222
    BBoolean=1
    BDate=2007-12-17
    BTime=22:06:23
    BDateTime=2007-12-17 22:06:23
    BDouble=3.14159265358979
  }
  List.Free;
end; //删除与添加
procedure TForm1.Button6Click(Sender: TObject);
begin
  ini.DeleteKey('BBB','BString');  {删除关键字}
  ini.EraseSection('CCC');        {删除小节}
//  ini.UpdateFile;                {保存到文件} {添加小节与关键字或修改值, 直接写入即可}
end; //其他功能
procedure TForm1.Button7Click(Sender: TObject);
var
  b: Boolean;
  s: string;
begin
  b := ini.SectionExists('DDD');        {判断某个小节是否存在}
  b := ini.ValueExists('AAA','AString'); {判断某个关键字的值是否存在}
  s := ini.FileName;                    {获取文件名}
end; procedure TForm1.FormDestroy(Sender: TObject);
begin
  ini.Free;
end; end.

ini 文件操作记要(1): 使用 TIniFile的更多相关文章

  1. delphi中ini 文件操作记要(1): 使用 TIniFile

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  2. ini 文件操作记要(2): 使用 TMemIniFile

    unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...

  3. ini文件操作

    Config.ini 文件操作 [SYS] sysname=hy company=hyhy tel=2 using System; using System.Collections.Generic; ...

  4. winform INI文件操作辅助类

    using System;using System.Runtime.InteropServices;using System.Text; namespace connectCMCC.Utils{ // ...

  5. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  6. Delphi ini文件操作 TIniFile、TMemIniFile

    1.使用TIniFile unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Co ...

  7. C# ini文件操作【源码下载】

    介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...

  8. C#读写ini文件操作

    ini文件,是windows操作系统下的配置文件,ini文件是一种按照特点方式排列的文本文件,它的构成分为三部分,结构如下: [Section1] key 1 = value2 key 1 = val ...

  9. C# Ini文件操作

    在开源中国看到的操作ini文件的,写的还不看,留着以后用 using System; using System.IO; using System.Runtime.InteropServices; us ...

随机推荐

  1. Java JDBC下执行SQL的不同方式、参数化预编译防御

    相关学习资料 http://zh.wikipedia.org/wiki/Java数据库连接 http://lavasoft.blog.51cto.com/62575/20588 http://blog ...

  2. Mac OS下编写对拍程序

    介绍 对拍是信息学竞赛中重要的技巧,它通过一个效率低下但正确率可以保证的程序,利用庞大的随机生成数据来验证我们的高级算法程序.对拍最大的优势在于可以通过人力所不能及的速度和数量达到验证的效果.下面我们 ...

  3. 修改eclipse/MyEclipse中包的显示结构为树形

    在右上边三角那里进去设置 选第一个是显示完整的包名,第二个显示的是树形结构,我们一般用第一种,如下图:

  4. boost构造,解析json

    void asynDBCenter::isGetActorInfoEx(void* on_process, const char* arg) { std::stringstream ros(arg); ...

  5. 用Nikto探测一个网站所用到的技术

    Nikto是一款开源的(GPL)网页服务器扫描器,它可以对网页服务器进行全面的多种扫描,包含超过3300种有潜在危险的文件/CGIs:超过 625种服务器版本:超过230种特定服务器问题,包括多种有潜 ...

  6. FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MINI-BATCH LEARNING. WHAT IS THE DIFFERENCE?

    FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MIN ...

  7. 织梦(dedecms) 5.7 /plus/car.php sql注入0day

    测试方法: @Sebug.net   dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! <?php $host=$argv[1]; $path=$argv[2]; $ ...

  8. mysql大表如何优化

    作者:哈哈链接:http://www.zhihu.com/question/19719997/answer/81930332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处 ...

  9. 清空file input框

    测试环境:OS --> winXPBrowsers --> IE6+, FF 3.0.11, FF 3.5, Opera 9.64, Opera 10 beta 2, Safari 4, ...

  10. struts2 + ajax + json的结合使用,实例讲解

    struts2用response怎么将json值返回到页面javascript解析,这里介绍一个struts2与json整合后包的用法. 1.准备工作 ①ajax使用Jquery:jquery-1.4 ...