Public File As StringPrivate 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 Integer, ByVal lpFileName As String) As Integer Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As IntegerSub DeleteSection(ByVal Section As String)Dim retval As Integer retval = WritePrivateProfileString(Section, 0&, "", File)End Sub Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)Dim retval As Integer SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)End FunctionPublic Function GetSetting(ByVal Section As String, ByVal KeyName As String) As StringDim retval As Integer Dim t As String * 255 ' Get the value retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File) ' If there is one, return it If retval > 0 Then GetSetting = Left$(t, retval) Else GetSetting = "Unknown section or key" End IfEnd FunctionPublic Function GetSection(ByVal Section As String, KeyArray() As String) As IntegerDim retval As Integer ' Allocate space for return value Dim t As String * 2500 Dim lastpointer As Integer Dim nullpointer As Integer Dim ArrayCount As Integer Dim keystring As String
ReDim KeyArray(0)
' Get the value retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File)
' If there is one, return it If retval > 0 Then ' ' Separate the keys and store them in the array nullpointer = InStr(t, Chr$(0)) lastpointer = 1 Do While (nullpointer <> 0 And nullpointer > lastpointer + 1) ' ' Extract key string keystring = Mid$(t, lastpointer, nullpointer - lastpointer) ' ' Now add to array ArrayCount = ArrayCount + 1 ReDim Preserve KeyArray(ArrayCount) KeyArray(ArrayCount) = keystring ' ' Find next null lastpointer = nullpointer + 1 nullpointer = InStr(nullpointer + 1, t, Chr$(0)) Loop End If ' ' Return the number of array elements GetSection = ArrayCount
我写的读写ini的模块: Option Explicit 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 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 'FileName:Ini文件 'PathName:小节名 'KeyName:值名 'WriteValue:值 Public Function WriteIni(FileName As String, _ PathName As String, _ KeyName As String, _ WriteValue As String) As Long Dim Rc As Long
End Function'FileName:Ini文件 'PathName:小节名 'KeyName:值名 'BackValue:返回值 'Default:默认字符 Public Function ReadIni(FileName As String, _ PathName As String, _ KeyName As String, _ BackValue As String, _ Optional Default As String = "缺省") As Long Dim Rc As Long Dim TempNum As String Dim TempStr As String
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As IntegerSub DeleteSection(ByVal Section As String)Dim retval As Integer retval = WritePrivateProfileString(Section, 0&, "", File)End Sub
Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)Dim retval As Integer SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)End FunctionPublic Function GetSetting(ByVal Section As String, ByVal KeyName As String) As StringDim retval As Integer
Dim t As String * 255 ' Get the value
retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File) ' If there is one, return it
If retval > 0 Then
GetSetting = Left$(t, retval)
Else
GetSetting = "Unknown section or key"
End IfEnd FunctionPublic Function GetSection(ByVal Section As String, KeyArray() As String) As IntegerDim retval As Integer
' Allocate space for return value
Dim t As String * 2500
Dim lastpointer As Integer
Dim nullpointer As Integer
Dim ArrayCount As Integer
Dim keystring As String
ReDim KeyArray(0)
' Get the value
retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File)
' If there is one, return it
If retval > 0 Then
'
' Separate the keys and store them in the array
nullpointer = InStr(t, Chr$(0))
lastpointer = 1
Do While (nullpointer <> 0 And nullpointer > lastpointer + 1)
'
' Extract key string
keystring = Mid$(t, lastpointer, nullpointer - lastpointer)
'
' Now add to array
ArrayCount = ArrayCount + 1
ReDim Preserve KeyArray(ArrayCount)
KeyArray(ArrayCount) = keystring
'
' Find next null
lastpointer = nullpointer + 1
nullpointer = InStr(nullpointer + 1, t, Chr$(0))
Loop
End If
'
' Return the number of array elements
GetSection = ArrayCount
End Function
Option Explicit
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 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
'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'WriteValue:值
Public Function WriteIni(FileName As String, _
PathName As String, _
KeyName As String, _
WriteValue As String) As Long
Dim Rc As Long
Rc = WritePrivateProfileString(PathName, KeyName, WriteValue, FileName)
WriteIni = Rc
End Function'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'BackValue:返回值
'Default:默认字符
Public Function ReadIni(FileName As String, _
PathName As String, _
KeyName As String, _
BackValue As String, _
Optional Default As String = "缺省") As Long
Dim Rc As Long
Dim TempNum As String
Dim TempStr As String
TempStr = String$(255, Chr$(0))
TempNum = 255
Rc = GetPrivateProfileString(PathName, KeyName, Default, TempStr, TempNum, FileName)
If Rc <> 0 Then
BackValue = Left$(TempStr, TempNum)
End If
ReadIni = Rc
End Function