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

📄 8控制ini文件的几种方法.htm

📁 对于学习很有帮助
💻 HTM
字号:
<html>

<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb_2312-80">
<meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0">
<title>DELPHI技术专栏</title>
<meta NAME="Template" CONTENT="C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\html.dot">
<link rel="stylesheet" href="../../cpcw.css">
</head>

<body LINK="#0000ff" VLINK="#800080" background="../bg.gif">

<table width="85%" border="0" align="center">
  <tr bgcolor="#FFFFFF">
    <td><p align="CENTER"><font color="#FF3333" class="title"><b>DELPHI技术专栏:控制INI文件的几种方法</b></font></p>
    <hr>
    <p>  在Windows中利用.INI文件做程序有关数据的存储工作是很常见的,其中涉及了读和写.INI文件问题,下面就介绍几种不同的方法给大家参考: 
    </p>
    <p><em><strong>从.INI文件中获取字符串</strong><br>
    </em><b><font color="#000000">var</font></b><br>
    <font color="#000000">strResult:pchar;<br>
    <b>begin</b><br>
    GetPrivateProfileString(</font><br>
    <font color="#008000">'windows'</font><font color="#000000">, </font><font color="#ff0000"><i>// 
    []中标题的名字</i></font><br>
    <font color="#008000">'NullPort'</font><font color="#000000">, </font><font
    color="#ff0000"><i>// =号前的名字</i></font><br>
    <font color="#008000">'NIL'</font><font color="#000000">, </font><font color="#ff0000"><i>// 
    如果没有找到字符串时,返回的默认值</i></font><br>
    <font color="#000000">strResult, </font><font color="#ff0000"><i>//存放取得字符</i></font><br>
    <font color="#0000ff">100</font><font color="#000000">, </font><font color="#ff0000"><i>//取得字符的允许最大长度</i></font><br>
    <font color="#008000">'c:\forwin95\win.ini'</font><font color="#000000"> </font><font
    color="#ff0000"><i>// 调用的文件名</i></font><br>
    <font color="#000000">);<br>
    edit1.text:=strResult; </font><font color="#ff0000"><i>//显示取得字符串</i></font></p>
    <p><em><strong>从.INI文件中获取整数</strong></em><br>
    <font color="#000000">edit1.text:=inttostr(GetPrivateProfileInt(</font><br>
    <font color="#008000">'intl'</font><font color="#000000">, </font><font color="#ff0000"><i>// 
    []中标题的名字</i></font><br>
    <font color="#008000">'iCountry'</font><font color="#000000">, </font><font
    color="#ff0000"><i>// =号前的名字</i></font><br>
    <font color="#0000ff">0</font><font color="#000000">,</font><font color="#ff0000"><i>// 
    如果没有找到整数时,返回的默认值</i></font><br>
    <font color="#008000">'c:\forwin95\win.ini'</font><font color="#000000"> </font><font
    color="#ff0000"><i>// 调用的文件名</i></font><br>
    <font color="#000000">));</font></p>
    <p><strong><em><b>向.INI文件写入字符串</b></em></strong><br>
    <font color="#000000">WritePrivateProfileString(</font><br>
    <font color="#008000">'windows'</font><font color="#000000">, </font><font color="#ff0000"><i>// 
    []中标题的名字</i></font><br>
    <font color="#008000">'load'</font><font color="#000000">, </font><font color="#ff0000"><i>// 
    要写入“=”号前的字符串</i></font><br>
    <font color="#008000">'accca'</font><font color="#000000">, </font><font color="#ff0000"><i>//要写入的数据</i></font><br>
    <font color="#008000">'c:\forwin95\win.ini'</font><font color="#000000"> </font><font
    color="#ff0000"><i>// 调用的文件名</i></font><br>
    <font color="#000000">);</font></p>
    <p><em><strong>向.INI文件写入整数</strong></em><br>
    <font color="#000000">WritePrivateProfileSection(</font><br>
    <font color="#008000">'windows'</font><font color="#000000">, </font><font color="#ff0000"><i>// 
    []中标题的名字</i></font><br>
    <font color="#008000">'read=100'</font><font color="#000000">, </font><font
    color="#ff0000"><i>// 要写入的数据</i></font><br>
    <font color="#008000">'c:\forwin95\win.ini'</font><font color="#000000"> </font><font
    color="#ff0000"><i>// 调用的文件名</i></font><br>
    <font color="#000000">);</font></p>
    <p>  上面的方法是调用API函数,下面介绍另一种不用API,而是使用<font
    color="#000000">TIniFile</font>从.INI文件中获取字符的方法</p>
    <p><strong><em><b>从.INI文件中读字符</b></em></strong><br>
    <font color="#000000"><b>var</b> MyIni: TIniFile;</font><br>
    <font color="#000000"><b>begin</b><br>
    MyIni := TIniFile.Create(</font><font color="#008000">'WIN.INI'</font><font
    color="#000000">);</font><font color="#ff0000"><i>//调用的文件名</i></font><br>
    <font color="#000000">edit1.text:=MyIni.ReadString(</font><font color="#008000">'Desktop'</font><font
    color="#000000">, </font><font color="#008000">'Wallpaper'</font><font color="#000000">, </font><font
    color="#008000">''</font><font color="#000000">);</font><font color="#ff0000"><i>//取得字符</i></font><br>
    <font color="#000000"><b>end</b>;</font></p>
    <p><em><strong>向.INI文件中写入字符</strong></em><br>
    <font color="#000000"><b>var</b> MyIni: TIniFile;</font><br>
    <font color="#000000"><b>begin</b><br>
    MyIni := TIniFile.Create(</font><font color="#008000">'WIN.INI'</font><font
    color="#000000">);</font><font color="#ff0000"><i>//调用的文件名</i></font><br>
    <font color="#000000">DelphiIni.WriteString(</font><font color="#008000">'Desktop'</font><font
    color="#000000">, </font><font color="#008000">'Wallpaper'</font><font color="#000000">, </font><font
    color="#008000">'c:\a.jpg'</font><font color="#000000">);</font><font color="#ff0000"><i>//写入字符</i></font><font
    color="#000000"><br>
    <b>end</b>;</font></p>
    <p>  下面的是本人自制的读INI文件函数,也提供给大家参考:</p>
    <p><font color="#000000"><b>function</b> GetINIfile(lpAppNameL,lpKeyName,lpDefault:<b>string</b>;<br>
    lpsize:<b>integer</b>;lpFileName:<b>string</b>):<b>string</b>;<br>
    </font><font color="#ff0000"><i>{读取ini文件函数}<br>
    </i></font><font color="#000000"><b>var</b> f:textfile;<br>
    sn:<b>string</b>;<br>
    <b>begin<br>
    </b>assignfile(f,lpFileName);<br>
    reset(f);<br>
    <b>repeat<br>
    </b>readln(f,sn);<br>
    <b>if</b> sn=</font><font color="#008000">'['</font><font color="#000000">+lpAppNameL+</font><font
    color="#008000">']'</font><font color="#000000"> <b>then<br>
    begin<br>
    </b>readln(f,sn);<br>
    <b>while</b>(copy(sn,</font><font color="#0000ff">1</font><font color="#000000">,</font><font
    color="#0000ff">1</font><font color="#000000">)&lt;&gt;</font><font color="#008000">'['</font><font
    color="#000000">)<b>or</b>(<b>not</b>(eof(f)))<b>do<br>
    begin<br>
    if</b> copy(sn,</font><font color="#0000ff">1</font><font color="#000000">,pos(</font><font
    color="#008000">'='</font><font color="#000000">,sn)-</font><font color="#0000ff">1</font><font
    color="#000000">)=lpKeyName <b>then<br>
    begin<br>
    </b>GetINIfile:=copy(sn,pos(</font><font color="#008000">'='</font><font color="#000000">,sn)+</font><font
    color="#0000ff">1</font><font color="#000000">,lpsize);<br>
    exit;<br>
    <b>end</b>;<br>
    readln(f,sn);<br>
    <b>end</b>;<br>
    <b>end<br>
    else</b> GetINIfile:=lpDefault;<br>
    <b>until</b> eof(f);<br>
    closefile(f);<br>
    <b>end</b>;<br>
    </font><font color="#ff0000"><i>{------------}</i></font> </p>
    <p><font color="#000000">  调用方法是:<br>
    <br>
    <b>var</b> Timeout:String;<br>
    <b>begin<br>
    </b>Timeout:=GetINIfile(</font><font color="#008000">'MailSetup'</font><font
    color="#000000">,</font><font color="#008000">'Timeout'</font><font color="#000000">,</font><font
    color="#008000">'0'</font><font color="#000000">,</font><font color="#0000ff">5</font><font
    color="#000000">,prgpath+</font><font color="#008000">'\CNMSet.ini'</font><font
    color="#000000">);<br>
    <b>end</b>;</font></td>
  </tr>
</table>
</body>
</html>

⌨️ 快捷键说明

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