Delphi- ini文件的读写操作
<p><strong>一、读INI文件示例</strong></p><div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TForm1.FormCreate(Sender: TObject);
Var
MyIni :Tinifile;
glAppPath :</span><span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
glAppPath :</span>= ExtractFilePath(Application.ExeName);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取当前运行程序的路径</span>
MyIni := Tinifile.<span style="color: rgba(0, 0, 255, 1)">Create</span>(glAppPath + <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">myini.ini</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
Edit1.Text :</span>= MyIni.ReadString(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">名称</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">);
Edit2.Text :</span>= MyIni.ReadString(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">地址</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">);
Edit3.Text :</span>= MyIni.ReadString(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">电话</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">);
MyIni.Free;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TForm1.N2Click(Sender: TObject);
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
Form2 :</span>= TForm2.<span style="color: rgba(0, 0, 255, 1)">Create</span><span style="color: rgba(0, 0, 0, 1)">(self);
Form2.ShowModal;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TForm1.Button1Click(Sender: TObject);
Var
MyIni :Tinifile;
glAppPath, path :</span><span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
glAppPath :</span>=<span style="color: rgba(0, 0, 0, 1)"> ExtractFilePath(Application.ExeName);
MyIni :</span>= Tinifile.<span style="color: rgba(0, 0, 255, 1)">Create</span>(glAppPath + <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">myini.ini</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
Edit1.Text :</span>= MyIni.ReadString(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">名称</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">);
Edit2.Text :</span>= MyIni.ReadString(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">地址</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">);
Edit3.Text :</span>= MyIni.ReadString(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">电话</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">);
MyIni.Free;
Path :</span>=<span style="color: rgba(0, 0, 0, 1)"> ExtractFilePath(Application.ExeName);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span>.</pre>
</div>
<p><strong>二、写ini文件示例</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TForm2.Button1Click(Sender: TObject);
Var
MyIni :Tinifile;
glAppPath :</span><span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
glAppPath :</span>=<span style="color: rgba(0, 0, 0, 1)"> ExtractFilePath(Application.ExeName);
MyIni :</span>= Tinifile.<span style="color: rgba(0, 0, 255, 1)">Create</span>(glAppPath + <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">myini.ini</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
MyIni.WriteString(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">名称</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,Edit1.text);
MyIni.WriteString(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">地址</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,Edit2.text);
MyIni.WriteString(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Setting</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">电话</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,Edit3.text);
MyIni.Free;
Close;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TForm2.Button2Click(Sender: TObject);
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
close;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span>.</pre>
</div>
<div>注意事项:</div>
<div>1.读写INI文件时要引用Inifiles单元</div>
<div>2. glAppPath := ExtractFilePath(Application.ExeName);获取当前运行程序的路径,记得要设置一下程序的输出路径才行否则程序路径是安装delphi的路径,设置方法:project->options->Directories/Coditionals的属性设为</div>
<div>Output directory :E:\prj\ini文件(即程序保存路径)</div>
<div>Unit output directory :E:\prj\ini文件(即程序保存路径)</div>
<div>MyIni := Tinifile.Create(glAppPath + 'myini.ini');</div>
<div> </div>
<div>3 ,myinin.ini结构如下 </div>
<div> (属性组名)</div>
<div>名称(属性名)=123(值)<br>地址=456<br>电话=789</div>
<div><br> </div>
<div data-find="_6">4.
<table data-find="_5">
<tbody data-find="_4">
<tr data-find="_3">
<td data-find="_2">
<div data-find="_1">利用Windows API函数WritePrivateProfileString 和GetPrivateProfileString可对.INI文件进行读写操作。其实,对.INI文件的读写完全可以利用Delphi的内置函数来实现。下面就介绍一些对.INI文件读写时相关的类及其属性方法。<br><br>1、TIniFile对象<br><br>以Delphi中,定义了一个TIniFile对象,将.INI文件封装在其中,并提供一些方法,专门用来对INI文件进行读写操作。如果在程序中要用到TIniFile类或其方法属性,就必须在程序单元的uses语句中手工加入对IniFiles单元的引用。<br><br>2、Create方法<br><br>该方法用来创建一个处理INI文件的TIniFile类型实例。<br><br>方法声明:constructor Create(const FileName:string);<br><br>参数说明:FileName指明待创建的INI文件的文件名;<br><br>注释:在使用TIniFile对象之前,必须先用此方法创建一个INI文件的实例。FileName中可以包含路径名,缺省时为Windows所在目录(一般就是C:\windows,对于Windows NT来说,则一般是c:\winnt)。用Create方法创建的实例,在使用完之后,调用Free方法释放内存。<br><br>3、ReadSection方法<br><br>该方法从INI文件中读出指定段的所有子键名,并存入Strings参数指定的字符串列表对象中。<br><br>方法声明:procedure ReadSection(const Section:string; Strings:TStrings);<br><br>参数说明:Section指明要读取段的段名;<br><br>Strings指明存放子键名的字符串列表;<br><br>注释:ReadSection方法仅读入指定段的所有子键名,但不读入子键的值。<br><br>4、ReadSections方法<br><br>该方法从INI文件中读取所有段名,并存入Strings参数指定的字符中列表中。<br><br>方法声明:procedure ReadSections(Strings:TStrings);<br><br>参数说明:Strings参数指明存放段名的字符串列表;<br><br>注释:ReadSections方法将INI文件中所有段的段名读出,存入一指定的字符串列表中,此字符串列表可以直接使用某个列表框的Items属性。<br><br>5、ReadSectionValues方法<br><br>该方法从INI文件中读入指定段的所有子键名及其键值,并存入Strings参数指定的字符串列表中。<br><br>方法声明:procedure ReadSectionValues(const Section:String; Strings:TStrings);<br><br>参数说明:Section指明要读取段的段名;<br><br>Strings指明存放段名的字符串列表;<br><br>注释:ReadSectionValues方法与ReadSection方法的区别在于后者仅读入子键名,面前者除了读取子键名之外,还读取该子键对应的键值。读入的子键名及键值在字符串列表中的存放方法与在文件中的显示方法一致,即"Key=Value"形式。<br><br>6、EraseSection方法<br><br>该方法删除INI文件中指定的一个整段。<br><br>方法声明:procedure EraseSection(const Section:string);<br><br>参数说明:Section指明待删除段的段名;<br><br>注释:EraseSection方法不仅删除指定段的段名,面且同时将该段的所有子键及键值删除。<br><br>7、DeleteKey方法<br><br>该方法删除指定段中的某个指定的子键。<br><br>方法声明:procedure DeleteKey(const Section,Key:string);<br><br>参数说明:Section指明待删除子键据段的段名;<br><br>Key指明待删除子键的键名;<br><br>注释:DeleteKey方法删除整个子键(包括键名和键值),也就是删除该子键所在的一行。<br><br>8、ReadBool方法<br><br>该方法读取指定段的某个子键的布尔值。<br><br>方法声明:function ReadBool(const Section,Key:string;Default:Boolean):Boolean;<br><br>参数说明:Section指明待读子键所在段的段名;<br><br>Key指明待读子键的键名;<br><br>Default参数指明缺省时的返回值。<br><br>注释:ReadBool方法用于读取一个子键的布尔型值,当键值为"1"时,返回True,键值为"0"时,返回False.<br><br>9、WriteBool方法<br><br>该方法向指定段的某个子键写入布尔值。<br><br>方法声明:procedure WriteBool(const Section, Key:string; Value:Boolean);<br><br>参数说明:Senction指明待写入子键所在段的段名;<br><br>Key参数指明待写入值的子键键名;<br><br>Value指明待写入的布尔值;<br><br>注释:WriteBool 方法用于写入一个子键的布尔值,当Value为"True"时,写入"1"。Value为"Flase"时,写入"0"。若在写入时,指定的段或键名不存在,则自动创建该段和键名。<br><br>10、ReadInteger方法<br><br>该方法读取指定段的某个子键的整型值。<br><br>方法声明:function ReadInteger(const Section,Key:string; Default:longint):longint;<br><br>注释:此方法与ReadBool方法类似,只是变量类型不同。<br><br>11、WriteInteger方法<br><br>该方法向指写段的某个子键写放整型值。<br><br>方法声明:procedure WriteInteger(const Section,Key:string; Value:longint);<br><br>注释:此方法与WriteBool方法类似,只是变量灰型不同。<br><br>12、ReadString方法<br><br>该方法读取指定段的某个子键的字符串型 值。<br><br>方法声明:function ReadString(const Section,Key:string; Default:string):string;<br><br>注释;此方法与ReadBool方法类似,只是变量类型不同。<br><br>13、WriteString方法<br><br>该方法向指写段的某个子键写入整型值。<br><br>方法声明:procedure WriteString(const Section,Key:string; Value:string);<br><br>注释:此方法与WriteBool方法类似,只是变量类型不同。<br><br>14、FileName属性<br><br>该属性指明被封装在TIniFile对象中的INI文件的文件名。<br><br>属性声明:property FileName:string;<br><br>注释:FileName属性是一个运行时的只读属性。<br><br>由一面的介绍,我们可以看到,强大的Delphi对INI文件的支持是非常全面的。我们在编写涉及此类操作的程序时,几乎无需使用Windows API函数</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="MySignature" role="contentinfo">
好的代码像粥一样,都是用时间熬出来的<br><br>
来源:https://www.cnblogs.com/jijm123/p/11291248.html
頁:
[1]