vb6.0 读取ini
时间: 2023-07-26 14:02:49 浏览: 75
在VB6.0中,我们可以使用以下代码来读取INI文件。
首先,需要在代码中添加以下声明以引用Windows API函数:
```
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
```
然后,可以使用以下代码来读取INI文件中的值:
```
Dim strValue As String
Dim strPath As String
Dim strDefault As String
Dim strAppName As String
Dim strKeyName As String
Dim lngSize As Long
' 指定INI文件路径
strPath = "C:\example.ini"
' 指定要读取的节、键和默认值
strAppName = "Section1"
strKeyName = "Key1"
strDefault = ""
' 初始化字符串缓冲区大小
lngSize = 255
' 创建字符串缓冲区
strValue = String(lngSize, vbNullChar)
' 调用API函数读取INI文件的值
Call GetPrivateProfileString(strAppName, strKeyName, strDefault, strValue, lngSize, strPath)
' 删除字符串缓冲区中的无关字符
strValue = Left$(strValue, InStr(strValue, vbNullChar) - 1)
' 输出读取到的值
MsgBox strValue
```
以上代码将读取INI文件中Section1下的Key1的值,并将其显示在一个消息框中。你可以根据需要修改strPath、strAppName和strKeyName来读取不同的INI文件和不同的键值。