Reader's Digest

Digest your Internet

使用INI文件

Written By: dch1 - May• 01•08
INI文件是Win16时代程序记录自身信息的方式,尽管当时的Windows 3.1也有注册表。在Win32时代,INI文件依然有其生存知道。比如一个绿色软件等等。
下面是读写INI文件的方法,为此我写了两个函数。
首先,声明两个API函数WritePrivateProfileString和GetPrivateProfileString,一下是代码:

Private Sub WriteToIni(ByVal Filename As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
Dim buff As String * 128
buff = Value + Chr(0)
WritePrivateProfileString Section, Key, buff, Filename
End Sub
Private Function ReadFromIni(ByVal Filename As String,ByVal Section As String, ByVal Key As String) As String
Dim i As Long
Dim buff As String * 128
GetPrivateProfileString Section, Key, “”, buff, 128, Filename
i = InStr(buff, Chr(0))
ReadFromIni = Trim(Left(buff, i – 1))
End Function
应用举例:
一、写INI文件
Dim Counter As Long
For Counter=1 To 4
Call WriteToIni(App.Path & “Options.ini”, “Test”, “Name” & Counter, “Value” & Counter)
Next Counter
运行后Options.ini内容如下:
[Test]
Name1=Value1
Name2=Value2
Name3=Value3
Name4=Value4
二、读INI文件
Dim Counter As Long
Dim Value(3) As String
For Counter=1 To 4
Value(Counter-1)=ReadFromIni(App.Path & “Options.ini”, “Test”, “Name” & Counter)
Next Counter

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.