zoukankan      html  css  js  c++  java
  • delphi中ini文件操作

    一,ini文件注释以‘;’开头

    二,定义

    1,在interface的uses节中添加inifiles;

    2,var   myinfile:tinifile;

    3,打开ini文件

    (1)myinifile:=tinifile.create('program.ini');

    (2)var filename:string;

             filename:=extractfilepath(paramstr(0))+'program.ini';     //0代表搜索到本项目的地址物理路径;

             myinifile:=tinifile.create(filename);

    //不指名路径的情况下文件会建在(c\windows目录中)

    三,读取

    procedure TForm2.btn2Click(Sender: TObject);
    var
    xiaojie:string;
    guanjian:string;
    begin
    xiaojie:=edt1.Text;
    guanjian:=edt2.Text;
    if (xiaojie<>'') and (guanjian<>'') then
    begin
    if cbb2.ItemIndex=0 then
    lbl6.Caption:=myinifile.ReadString(xiaojie,guanjian,缺省值)     //缺省值代表ini不存在该关键字时返回值
    else if cbb2.ItemIndex=1 then
    bl6.Caption:=IntToStr(myinifile.ReadInteger(xiaojie,guanjian,0))
    else if cbb2.ItemIndex=2 then
    cbb1.ItemIndex:=Integer(myinifile.ReadBool(xiaojie,guanjian,False))
    end;
    myinifile.Free;
    end;

    四,写入

    procedure TForm2.btn1Click(Sender: TObject);
    var
    xiaojie:string;
    guanjian:string;
    zhi:string;
    zhi1:Integer;
    zhi2:Boolean;
    begin
    xiaojie:=edt1.Text;
    guanjian:=edt2.Text;
    zhi:=edt3.Text;
    zhi1:=StrToInt(edt4.Text);
    zhi2:=Boolean(cbb1.ItemIndex);
    if edt4.text<>'' then
    myinifile.WriteString(xiaojie,guanjian,zhi);
    if edt4.text<>'' then
    myinifile.WriteInteger(xiaojie,guanjian,zhi1);
    if (cbb1.ItemIndex=0) or (cbb1.ItemIndex=1) then
    myinifile.WriteBool(xiaojie,guanjian,zhi2);
    myinifile.Free;
    end;

    //当这个文件不存在时,上面语句还会自动创建ini文件

    五,删除关键字

    myinifile.deletekey('小节名','关键字');

    六,删除小节

    myinifile.erasesection('小节名');

    七,小节操作

    myinifile.readsection('小节名',tstring 变量);  //可将指定小节中所有关键字名读取至一个字符串列表变量中;

    myinifile.readsections(tstring 变量);     //可将ini文件中所有小节名读取至一个字符串列表变量中去;

    myinifile.readsectionvalues('小节名',tstring 变量);     //可将ini文件中指定小节所有行(包括关键字,=,值)读取至一个字符串变量中去;

    八,释放

    myinifile.destory;

  • 相关阅读:
    ARP:地址解析协议,RARP
    pip 安装psutil 报错 error: command 'gcc' failed with exit status 1
    linux shell 控制脚本
    linux shell 呈现数据
    linux shell 处理用户输入
    shell结构化命令
    centos7 安装配置 squid作为正向代理
    linux基本脚本
    linux文件权限
    模拟垃圾分布
  • 原文地址:https://www.cnblogs.com/hanhan/p/2428280.html
Copyright © 2011-2022 走看看