vb6.0读取 ini 代码
新手不懂,麻烦大神们帮分析下....我现在用的这个INI函数是正常的,现在我只会写,不知道怎么读..由于有文字数量限制,函数部份我只发读取的.窗口内这样是可以读取的:Di...
新手不懂,麻烦大神们帮分析下....
我现在用的这个INI函数是正常的,现在我只会写,不知道怎么读..
由于有文字数量限制,函数部份我只发读取的.
窗口内这样是可以读取的:
Dim i As Long
Dim A() As String
' A = GetInfoSection("配置", App.Path & "\config.ini")
' For i = 1 To UBound(A)
' MsgBox A(i)
但是我想在textbox中读取就不会了.
'Text1.Text = GetFromINI("配置", "text1", App.Path & "\config.ini")
A = GetStr("配置", App.Path & "\config.ini", "text1", "text1.text")
上述的代码是错的,应该是什么?
函数读取部份为:
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 pFileName 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
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
Public Function GetIniStr(ByVal FileName As String, ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, FileName
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
'Option Explicit
'遍历ini文件 , 某个主键下的所有Key
Public Function GetInfoSection(strSection As String, strIniFile As String) As String()
Dim strReturn As String * 32767
Dim strTmp As String
Dim nStart As Integer
Dim nEnd As Integer
Dim i As Integer
Dim sArray() As String
Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn), strIniFile)
strTmp = strReturn
i = 1
Do While strTmp <> ""
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
我的QQ是:10415858 懂的麻烦帮我解决下. 展开
我现在用的这个INI函数是正常的,现在我只会写,不知道怎么读..
由于有文字数量限制,函数部份我只发读取的.
窗口内这样是可以读取的:
Dim i As Long
Dim A() As String
' A = GetInfoSection("配置", App.Path & "\config.ini")
' For i = 1 To UBound(A)
' MsgBox A(i)
但是我想在textbox中读取就不会了.
'Text1.Text = GetFromINI("配置", "text1", App.Path & "\config.ini")
A = GetStr("配置", App.Path & "\config.ini", "text1", "text1.text")
上述的代码是错的,应该是什么?
函数读取部份为:
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 pFileName 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
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
Public Function GetIniStr(ByVal FileName As String, ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, FileName
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
'Option Explicit
'遍历ini文件 , 某个主键下的所有Key
Public Function GetInfoSection(strSection As String, strIniFile As String) As String()
Dim strReturn As String * 32767
Dim strTmp As String
Dim nStart As Integer
Dim nEnd As Integer
Dim i As Integer
Dim sArray() As String
Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn), strIniFile)
strTmp = strReturn
i = 1
Do While strTmp <> ""
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
我的QQ是:10415858 懂的麻烦帮我解决下. 展开
1个回答
展开全部
你的代码太乱,没看。给你个现成的函数
'API声明
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As Long
'读取INI函数
Public Function PutFromINI(AppName As String, KeyName As String, KeyText As String, Filename As String) As Boolean
Dim r As Long
r = WritePrivateProfileString(AppName, KeyName, KeyText, Filename)
If r <> 1 Then
MsgBox "写配置文件出错!", vbExclamation
PutFromINI = False
Else
PutFromINI = True
End If
End Function
'调用方法:
Call PutFromINI("配置", "值", text1.text, app.path & "myinifile.ini")
'API声明
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As Long
'读取INI函数
Public Function PutFromINI(AppName As String, KeyName As String, KeyText As String, Filename As String) As Boolean
Dim r As Long
r = WritePrivateProfileString(AppName, KeyName, KeyText, Filename)
If r <> 1 Then
MsgBox "写配置文件出错!", vbExclamation
PutFromINI = False
Else
PutFromINI = True
End If
End Function
'调用方法:
Call PutFromINI("配置", "值", text1.text, app.path & "myinifile.ini")
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询