下面是读写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