在 VBA 中读写 .ini 文件的方法主要涉及到文件的读写操作,以下是一个简单的示例代码:
Sub ReadWriteIniFile()
Dim iniPath As String
Dim section As String
Dim key As String
Dim value As String
' 设置 ini 文件路径
iniPath = "C:\example.ini"
' 设置要读取的 section 和 key
section = "Section1"
key = "Key1"
' 读取 ini 文件中的值
value = ReadIniFile(iniPath, section, key)
' 输出读取的值
Debug.Print "Value: " & value
' 设置要写入的 section 和 key 以及值
section = "Section2"
key = "Key2"
value = "Value2"
' 写入值到 ini 文件
WriteIniFile iniPath, section, key, value
End Sub
Function ReadIniFile(ByVal iniPath As String, ByVal section As String, ByVal key As String) As String
Dim fso As Object
Dim iniFile As Object
Dim sections As Object
Dim keys As Object
Dim value As String
' 创建文件系统对象
Set fso = CreateObject("Scripting.FileSystemObject")
' 检查文件是否存在
If Not fso.FileExists(iniPath) Then
Exit Function
End If
' 打开 ini 文件
Set iniFile = fso.OpenTextFile(iniPath, 1)
' 读取文件内容
Do While Not iniFile.AtEndOfStream
Dim line As String
line = iniFile.ReadLine
' 检查是否为 section
If Left(line, 1) = "[" And Right(line, 1) = "]" Then
Set sections = Nothing
Set keys = Nothing
value = ""
End If
' 检查是否为 key
If InStr(line, "=") > 0 Then
Dim parts As Variant
parts = Split(line, "=")
' 如果是当前 section 和 key,则保存值
If sections Is Nothing Or sections.Exists(section) Then
If keys Is Nothing Or keys.Exists(key) Then
value = parts(1)
End If
End If
' 添加 key 到当前 section
If sections Is Nothing Or sections.Exists(section) Then
keys(key) = ""
End If
End If
' 如果是新的 section,则保存到 sections 集合中
If sections Is Nothing And Left(line, 1) = "[" And Right(line, 1) = "]" Then
Set sections = CreateObject("Scripting.Dictionary")
sections(section) = ""
End If
Loop
' 关闭文件
iniFile.Close
' 返回读取的值
ReadIniFile = value
End Function
Sub WriteIniFile(ByVal iniPath As String, ByVal section As String, ByVal key As String, ByVal value As String)
Dim fso As Object
Dim iniFile As Object
Dim sections As Object
Dim keys As Object
Dim foundSection As Boolean
Dim foundKey As Boolean
' 创建文件系统对象
Set fso = CreateObject("Scripting.FileSystemObject")
' 检查文件是否存在
If Not fso.FileExists(iniPath) Then
Exit Sub
End If
' 打开 ini 文件
Set iniFile = fso.OpenTextFile(iniPath, 1)
' 读取文件内容
Do While Not iniFile.AtEndOfStream
Dim line As String
line = iniFile.ReadLine
' 检查是否为 section
If Left(line, 1) = "[" And Right(line, 1) = "]" Then
Set sections = Nothing
Set keys = Nothing
foundSection = False
foundKey = False
End If
' 检查是否为 key
If InStr(line, "=") > 0 Then
Dim parts As Variant
parts = Split(line, "=")
' 如果是当前 section 和 key,则保存值
If sections Is Nothing Or sections.Exists(section) Then
If keys Is Nothing Or keys.Exists(key) Then
foundKey = True
If parts(1) <> value Then
' 如果值不同,则更新值
iniFile.Position = iniFile.Position - Len(line) - 2
iniFile.Write key & "=" & value
Exit Do
End If
End If
End If
' 添加 key 到当前 section
If sections Is Nothing Or sections.Exists(section) Then
keys(key) = ""
End If
End If
' 如果是新的 section,则保存到 sections 集合中
If sections Is Nothing And Left(line, 1) = "[" And Right(line, 1) = "]" Then
Set sections = CreateObject("Scripting.Dictionary")
sections(section) = ""
If section = section Then
foundSection = True
End If
End If
Loop
' 如果没有找到 section,则添加到文件末尾
If Not foundSection Then
iniFile.Write vbNewLine & "[" & section & "]" & vbNewLine
foundSection = True
End If
' 如果没有找到 key,则添加到文件末尾
If Not foundKey Then
iniFile.Write key & "=" & value & vbNewLine
End If
' 关闭文件
iniFile.Close
End Sub
这个示例代码中,ReadIniFile
函数用于从 .ini
领取专属 10元无门槛券
手把手带您无忧上云