第1个回答 2010-09-21
建立一个模块(INI文件操作.bas),复制以下代码:
Option Explicit
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
Private sPathFile As String
Public Property Get PathFile() As String
Dim sPath As String
If sPathFile = vbNullString Then
sPath = App.Path
If Right(sPath, 1) = "\" Then
sPath = sPath & "\"
End If
PathFile = sPath & App.EXEName & ".ini"
Else
PathFile = sPathFile
End If
End Property
Public Property Let PathFile(sNewPathFile As String)
sPathFile = sNewPathFile
End Property
Public Function GetValue(ByVal sKey As String, Optional ByVal sDefault As String) As String
GetValue = GetValueEx("参数设置", sKey, sDefault)
End Function
Public Function GetValueEx(ByVal sModule As String, ByVal sKey As String, Optional ByVal sDefault As String) As String
GetValueEx = GetValueFromFile(PathFile, sModule, sKey, sDefault)
End Function
Public Function GetValueFromFile(ByVal sFile As String, ByVal sModule As String, ByVal sKey As String, Optional ByVal sDefault As String) As String
Dim sRtn As String
'If (Not Dir(sFile) = vbNullString) And (Not Trim(sFile) = vbNullString) Then
sRtn = Space(255)
GetPrivateProfileString sModule, sKey, sDefault, sRtn, 255, sFile
GetValueFromFile = BTrim(sRtn)
'End If
End Function
Public Function SetValue(ByVal sKey As String, ByVal sValue As String) As Boolean
SetValue = SetValueEx("参数设置", sKey, sValue)
End Function
Public Function SetValueEx(ByVal sModule As String, ByVal sKey As String, ByVal sValue As String) As Boolean
SetValueEx = SetValueToFile(PathFile, sModule, sKey, sValue)
End Function
Public Function SetValueToFile(ByVal sFile As String, ByVal sModule As String, ByVal sKey As String, ByVal sValue As String) As Boolean
SetValueToFile = WritePrivateProfileString(sModule, sKey, sValue, sFile)
End Function
Private Function BTrim(sStr As String) As String
Dim sRtn As String
Dim i As Long
Dim sChar As String
sRtn = sStr
For i = 1 To Len(sStr)
sChar = Mid$(sStr, i, 1)
If sChar = Chr$(0) Then
sRtn = Left$(sStr, i - 1)
Exit For
End If
Next i
sRtn = Trim(sRtn)
BTrim = sRtn
End Function
'这段代码我在产品里用了十多年了,可放心使用。如果你会用GetSetting和SaveSetting语句,接受这个就不难,例如“GetValue("b100","IP地址","默认值")”,sPathFile要先设置路径文件名。
第2个回答 2010-09-22
ok,已经按你的描述改了代码,试一下行不行吧。我这里测试的可以。这里假设配置文件在工程目录下,名字为ip.ini
Private Sub Command1_Click()
Dim a As Variant
a = GetSections(App.Path & "\ip.ini")
Dim i As Long
For i = 0 To UBound(a) - 1
List1.AddItem a(i)
Next
End Sub
Public Function GetSections(iniFileName As String) As String()
Dim Sections() As String, Count As Long
Dim lnStr As String
Open iniFileName For Input As #1
Do While Not EOF(1)
Line Input #1, lnStr
If Left(lnStr, 1) = "[" And Right(lnStr, 1) = "]" Then
ReDim Preserve Sections(0 To Count + 1)
Sections(Count) = Mid(lnStr, 2, Len(lnStr) - 2)
Count = Count + 1
End If
Loop
Close #1
GetSections = Sections
End Function本回答被提问者采纳