说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

VB使用INI文件

HTML文档下载 WORD文档下载 PDF文档下载
VB使用INI文件
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
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘