vb怎么读INI文件内容啊!
能不能给个例子.那人API实在是难看懂的~
谢谢了~
[解决办法]
使用这两个API
Private Declare Function WritePrivateProfileString Lib "kernel32 " Alias "WritePrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
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的函数
'写INI文件的方法
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
'读INI文件的方法
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模块
---------------------------
Option Explicit
Private Declare Function GetPrivateProfileSection Lib "kernel32 " Alias "GetPrivateProfileSectionA " (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'返回一个字符串数组
'调用举例:
'获得指定ini文件中某个节下面某个子键的键值,需要下面的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
Private Declare Function WritePrivateProfileString Lib "kernel32 " Alias _
"WritePrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
lpString As Any, ByVal lpFileName As String) As Long
'返回一个字符串
Public arrClass() As String
Public SoftListArr() As String
Public GetIniArr As Integer
Public Function GetiniValue(ByVal lpKeyName As String, ByVal strName As String, ByVal strIniFile As String) As String
Dim strTmp As String * 32767
Call GetPrivateProfileString(lpKeyName, strName, " ", _
strTmp, Len(strTmp), strIniFile)
GetiniValue = Left$(strTmp, InStr(strTmp, vbNullChar) - 1)
End Function
Public Function GetInfoSection(strSection As String, strIniFile As String) As String()
Dim strReturn As String * 32767
Dim strTmp As String
Dim nStart As Integer, nEnd As Integer, i As Integer
Dim sArray() As String
Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn), strIniFile)
'strReturn = Right(strReturn, Len(strReturn) - 2)
'If Len(strReturn) = 3 Then strReturn = " "
strTmp = strReturn 'Mid(strReturn, InStr(1, strReturn, "= ") + 1, Len(strReturn))
i = 1
Do While strTmp <> " " And Len(strTmp) <> 32765
nStart = nEnd + 1
nEnd = InStr(nStart, strReturn, vbNullChar)
strTmp = Mid$(strReturn, nStart, nEnd - nStart)
If Len(strTmp) > 0 Then
ReDim Preserve sArray(1 To i)
sArray(i) = strTmp
i = i + 1
End If
Loop
GetInfoSection = sArray
End Function
Public Function zqTrim(ByVal strSour As String) As String
Dim strTmp As String
Dim nLen As Integer
Dim i As Integer, j As Integer
Dim strNow As String, strValid() As String, strNew As String
'strNow 当前字符
'strValid 有效字符
'strNew 最后生成的新字符
strTmp = Trim$(strSour)
nLen = Len(strTmp)
If nLen < 1 Then
zqTrim = " "
Exit Function
End If
j = 0
For i = 1 To nLen
strNow = Mid(strTmp, i, 1) '每次读取一个字符
'MsgBox Asc(strNow)
If strNow <> vbNullChar And Asc(strNow) <> 9 Then '如果有效,则存入有效数组
ReDim Preserve strValid(j)
strValid(j) = strNow
j = j + 1
End If
Next i
strNew = Join(strValid, " ") '将所有有效字符连接起来
zqTrim = Trim$(strNew) '去掉字符串中的首尾空格
End Function
Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String, ByVal strIniFile As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = " " Or VBA.Trim(In_Key) = " " Or VBA.Trim(AppName) = " " Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, strIniFile
End If
Exit Function
WriteIniStrErr:
err.Clear
WriteIniStr = False
End Function
Public Function GetRestrictInfo(ByVal configText As String, ByVal configFilePath As String) As String()
Dim TmpStr() As String, strarr() As String
Dim i As Long
TmpStr = GetInfoSection(configText, configFilePath)
On Error GoTo err
For i = 1 To UBound(TmpStr)
ReDim Preserve strarr(1 To i)
strarr(i) = Left(TmpStr(i), InStr(1, TmpStr(i), "= ") - 1)
Next
GetRestrictInfo = strarr
Exit Function
err:
GetIniArr = 1
End Function
Public Function GetRestrictInfoEx(ByVal configText As String, ByVal configFilePath As String) As String()
Dim TmpStr() As String, strarr() As String
Dim i As Long
TmpStr = GetInfoSection(configText, configFilePath)
On Error GoTo err
For i = 1 To UBound(TmpStr)
ReDim Preserve strarr(1 To i)
strarr(i) = Mid(TmpStr(i), InStr(1, TmpStr(i), "= ") + 1, Len(TmpStr(i)))
Next
GetRestrictInfoEx = strarr
Exit Function
err:
GetIniArr = 1
End Function
---------------使用方法----------
Dim Filename() As String
Filename= GetRestrictInfoEx( "Path ", App.Path & "\cfg.ini ")
msgbox filename(1)
..... .. (2)