WritePrivateProfileString GetPrivateProfileString Public 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 GetProfileString Lib "kernel32" Alias "GetProfileStringA" _ (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault _ As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Attribute VB_Name = "CIniFile" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = False Option Explicit Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, 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 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 LongPrivate IniFileName As String Public ErrorMsg As String Private Sub Class_Initialize() IniFileName = vbNullString ErrorMsg = vbNullString End Sub
Public Sub SpecifyIni(FilePathName) IniFileName = Trim(FilePathName) End Sub
Private Function NoIniFile() As Boolean NoIniFile = True If IniFileName = vbNullString Then ErrorMsg = "テサモミヨクカィ INI ホトシ" Exit Function End If ErrorMsg = vbNullString NoIniFile = False End Function
Public Function WriteString(Section As String, key As String, Value As String) As Boolean WriteString = False If NoIniFile() Then Exit Function End If If WritePrivateProfileString(Section, key, Value, IniFileName) = 0 Then ErrorMsg = "ミエネ・ァーワ" Exit Function End If WriteString = True End Function Public Function ReadString(Section As String, key As String, Size As Long) As String Dim ReturnStr As String Dim ReturnLng As Long ReadString = vbNullString If NoIniFile() Then Exit Function End If ReturnStr = Space(Size) ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, IniFileName) ReadString = VBA.Left(ReturnStr, ReturnLng) End Function
Public Function ReadInt(Section As String, key As String) As Long Dim ReturnLng As Long ReadInt = 0 ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName) If ReturnLng = 0 Then ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName) If ReturnLng = 1 Then ErrorMsg = "イサトワカチネ。" Exit Function End If End If ReadInt = ReturnLng End Function
给你两个模块 Option ExplicitPrivate 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 GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName 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 LongDim i As Integer, Buffer As StringPublic Function GetValue(fileName As String, Section As String, Name As String) As String '读取INI文件值 On Error GoTo er Dim s As String s = String(1024, 0) s = Left(s, Val(GetPrivateProfileString(Section, Name, "", s, Len(s), fileName))) i = InStr(s, Chr(0)) If i <> 0 Then GetValue = Left(s, i - 1) Else GetValue = s er: If Err.Number <> 0 Then GetValue = vbNullChar End FunctionPublic Function DeleteSection(fileName As String, Section As String) As Long '删除指定节点 DeleteSection = WritePrivateProfileString(Section, vbNullString, vbNullString, fileName) End FunctionPublic Function DeleteValue(fileName As String, Section As String, Name As String) As Long '删除指定键名 DeleteValue = WritePrivateProfileString(Section, Name, vbNullString, fileName) End FunctionPublic Function SetValue(fileName As String, Section As String, Name As String, Value As String) As Long '设置指定键的值 SetValue = WritePrivateProfileString(Section, Name, Value, fileName) End FunctionPublic Function EnumValue(fileName As String, Section As String, Key() As String) As Long '获取指定节点下的所有键 On Error GoTo er Dim ss As String Dim pos As Integer, Count As Integer Buffer = String(32767, 0) GetPrivateProfileSection Section, Buffer, Len(Buffer), fileName pos = InStr(Buffer, Chr(0)) Do ReDim Preserve Key(Count) ss = Left(Buffer, pos - 1) Key(Count) = Left(ss, InStr(1, ss, "=") - 1) Buffer = Mid(Buffer, pos + 1) pos = InStr(Buffer, Chr(0)) If pos <= 1 Then Exit Do Count = Count + 1 Loop EnumValue = Count Exit Function er: ReDim Key(0) EnumValue = 0 End FunctionPublic Function EnumSection(fileName As String, Section() As String) As Long '获取所有节点 On Error GoTo er Dim Length As Long, pos As Integer, Count As Integer Buffer = String(32767, 0) Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName) Buffer = Left(Buffer, Length) Do ReDim Preserve Section(Count) pos = InStr(Buffer, Chr(0)) If Left(Buffer, pos - 1) = "" Then Exit Do Section(Count) = Left(Buffer, pos - 1) Buffer = Mid(Buffer, pos + 1) If Len(Buffer) <= 0 Then Exit Do Count = Count + 1 Loop EnumSection = Count Exit Function er: ReDim Section(0) EnumSection = 0 End FunctionPublic Function SectionExists(fileName As String, Section As String) As Boolean On Error GoTo er Dim Length As Long, pos As Integer, Count As Integer Buffer = String(32767, 0) Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName) Buffer = Left(Buffer, Length) Do pos = InStr(Buffer, Chr(0)) If Left(Buffer, pos - 1) = "" Then Exit Do If UCase(Left(Buffer, pos - 1)) = UCase(Section) Then SectionExists = True: Exit Do Buffer = Mid(Buffer, pos + 1) If Len(Buffer) <= 0 Then Exit Do Loop Exit Function er: SectionExists = False End Function
Option ExplicitPrivate 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 GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName 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 LongDim i As Integer, Buffer As StringPublic Function GetValue(fileName As String, Section As String, Name As String) As String '读取INI文件值 On Error GoTo er Dim s As String s = String(1024, 0) s = Left(s, Val(GetPrivateProfileString(Section, Name, "", s, Len(s), fileName))) i = InStr(s, Chr(0)) If i <> 0 Then GetValue = Left(s, i - 1) Else GetValue = s er: If Err.Number <> 0 Then GetValue = vbNullChar End FunctionPublic Function DeleteSection(fileName As String, Section As String) As Long '删除指定节点 DeleteSection = WritePrivateProfileString(Section, vbNullString, vbNullString, fileName) End FunctionPublic Function DeleteValue(fileName As String, Section As String, Name As String) As Long '删除指定键名 DeleteValue = WritePrivateProfileString(Section, Name, vbNullString, fileName) End FunctionPublic Function SetValue(fileName As String, Section As String, Name As String, Value As String) As Long '设置指定键的值 SetValue = WritePrivateProfileString(Section, Name, Value, fileName) End FunctionPublic Function EnumValue(fileName As String, Section As String, Key() As String) As Long '获取指定节点下的所有键 On Error GoTo er Dim ss As String Dim pos As Integer, Count As Integer Buffer = String(32767, 0) GetPrivateProfileSection Section, Buffer, Len(Buffer), fileName pos = InStr(Buffer, Chr(0)) Do ReDim Preserve Key(Count) ss = Left(Buffer, pos - 1) Key(Count) = Left(ss, InStr(1, ss, "=") - 1) Buffer = Mid(Buffer, pos + 1) pos = InStr(Buffer, Chr(0)) If pos <= 1 Then Exit Do Count = Count + 1 Loop EnumValue = Count Exit Function er: ReDim Key(0) EnumValue = 0 End FunctionPublic Function EnumSection(fileName As String, Section() As String) As Long '获取所有节点 On Error GoTo er Dim Length As Long, pos As Integer, Count As Integer Buffer = String(32767, 0) Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName) Buffer = Left(Buffer, Length) Do ReDim Preserve Section(Count) pos = InStr(Buffer, Chr(0)) If Left(Buffer, pos - 1) = "" Then Exit Do Section(Count) = Left(Buffer, pos - 1) Buffer = Mid(Buffer, pos + 1) If Len(Buffer) <= 0 Then Exit Do Count = Count + 1 Loop EnumSection = Count Exit Function er: ReDim Section(0) EnumSection = 0 End FunctionPublic Function SectionExists(fileName As String, Section As String) As Boolean On Error GoTo er Dim Length As Long, pos As Integer, Count As Integer Buffer = String(32767, 0) Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName) Buffer = Left(Buffer, Length) Do pos = InStr(Buffer, Chr(0)) If Left(Buffer, pos - 1) = "" Then Exit Do If UCase(Left(Buffer, pos - 1)) = UCase(Section) Then SectionExists = True: Exit Do Buffer = Mid(Buffer, pos + 1) If Len(Buffer) <= 0 Then Exit Do Loop Exit Function er: SectionExists = False End Function 哪个高手用此模块给个详细的例子来操作一下 新建INI、新建值、和修改、删除这些功能吧!初学啊,不知道怎么调用!
GetPrivateProfileString
Public 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 GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault _
As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, 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
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 LongPrivate IniFileName As String
Public ErrorMsg As String
Private Sub Class_Initialize()
IniFileName = vbNullString
ErrorMsg = vbNullString
End Sub
Public Sub SpecifyIni(FilePathName)
IniFileName = Trim(FilePathName)
End Sub
Private Function NoIniFile() As Boolean
NoIniFile = True
If IniFileName = vbNullString Then
ErrorMsg = "テサモミヨクカィ INI ホトシ"
Exit Function
End If
ErrorMsg = vbNullString
NoIniFile = False
End Function
Public Function WriteString(Section As String, key As String, Value As String) As Boolean
WriteString = False
If NoIniFile() Then
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, IniFileName) = 0 Then
ErrorMsg = "ミエネ・ァーワ"
Exit Function
End If
WriteString = True
End Function Public Function ReadString(Section As String, key As String, Size As Long) As String
Dim ReturnStr As String
Dim ReturnLng As Long
ReadString = vbNullString
If NoIniFile() Then
Exit Function
End If
ReturnStr = Space(Size)
ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, IniFileName)
ReadString = VBA.Left(ReturnStr, ReturnLng)
End Function
Public Function ReadInt(Section As String, key As String) As Long
Dim ReturnLng As Long
ReadInt = 0
ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName)
If ReturnLng = 1 Then
ErrorMsg = "イサトワカチネ。"
Exit Function
End If
End If
ReadInt = ReturnLng
End Function
Option ExplicitPrivate 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 GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName 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 LongDim i As Integer, Buffer As StringPublic Function GetValue(fileName As String, Section As String, Name As String) As String
'读取INI文件值
On Error GoTo er
Dim s As String
s = String(1024, 0)
s = Left(s, Val(GetPrivateProfileString(Section, Name, "", s, Len(s), fileName)))
i = InStr(s, Chr(0))
If i <> 0 Then GetValue = Left(s, i - 1) Else GetValue = s
er:
If Err.Number <> 0 Then GetValue = vbNullChar
End FunctionPublic Function DeleteSection(fileName As String, Section As String) As Long
'删除指定节点
DeleteSection = WritePrivateProfileString(Section, vbNullString, vbNullString, fileName)
End FunctionPublic Function DeleteValue(fileName As String, Section As String, Name As String) As Long
'删除指定键名
DeleteValue = WritePrivateProfileString(Section, Name, vbNullString, fileName)
End FunctionPublic Function SetValue(fileName As String, Section As String, Name As String, Value As String) As Long
'设置指定键的值
SetValue = WritePrivateProfileString(Section, Name, Value, fileName)
End FunctionPublic Function EnumValue(fileName As String, Section As String, Key() As String) As Long
'获取指定节点下的所有键
On Error GoTo er
Dim ss As String
Dim pos As Integer, Count As Integer
Buffer = String(32767, 0)
GetPrivateProfileSection Section, Buffer, Len(Buffer), fileName
pos = InStr(Buffer, Chr(0))
Do
ReDim Preserve Key(Count)
ss = Left(Buffer, pos - 1)
Key(Count) = Left(ss, InStr(1, ss, "=") - 1)
Buffer = Mid(Buffer, pos + 1)
pos = InStr(Buffer, Chr(0))
If pos <= 1 Then Exit Do
Count = Count + 1
Loop
EnumValue = Count
Exit Function
er:
ReDim Key(0)
EnumValue = 0
End FunctionPublic Function EnumSection(fileName As String, Section() As String) As Long
'获取所有节点
On Error GoTo er
Dim Length As Long, pos As Integer, Count As Integer
Buffer = String(32767, 0)
Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName)
Buffer = Left(Buffer, Length)
Do
ReDim Preserve Section(Count)
pos = InStr(Buffer, Chr(0))
If Left(Buffer, pos - 1) = "" Then Exit Do
Section(Count) = Left(Buffer, pos - 1)
Buffer = Mid(Buffer, pos + 1)
If Len(Buffer) <= 0 Then Exit Do
Count = Count + 1
Loop
EnumSection = Count
Exit Function
er:
ReDim Section(0)
EnumSection = 0
End FunctionPublic Function SectionExists(fileName As String, Section As String) As Boolean
On Error GoTo er
Dim Length As Long, pos As Integer, Count As Integer
Buffer = String(32767, 0)
Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName)
Buffer = Left(Buffer, Length)
Do
pos = InStr(Buffer, Chr(0))
If Left(Buffer, pos - 1) = "" Then Exit Do
If UCase(Left(Buffer, pos - 1)) = UCase(Section) Then SectionExists = True: Exit Do
Buffer = Mid(Buffer, pos + 1)
If Len(Buffer) <= 0 Then Exit Do
Loop
Exit Function
er:
SectionExists = False
End Function
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
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 LongDim i As Integer, Buffer As StringPublic Function GetValue(fileName As String, Section As String, Name As String) As String
'读取INI文件值
On Error GoTo er
Dim s As String
s = String(1024, 0)
s = Left(s, Val(GetPrivateProfileString(Section, Name, "", s, Len(s), fileName)))
i = InStr(s, Chr(0))
If i <> 0 Then GetValue = Left(s, i - 1) Else GetValue = s
er:
If Err.Number <> 0 Then GetValue = vbNullChar
End FunctionPublic Function DeleteSection(fileName As String, Section As String) As Long
'删除指定节点
DeleteSection = WritePrivateProfileString(Section, vbNullString, vbNullString, fileName)
End FunctionPublic Function DeleteValue(fileName As String, Section As String, Name As String) As Long
'删除指定键名
DeleteValue = WritePrivateProfileString(Section, Name, vbNullString, fileName)
End FunctionPublic Function SetValue(fileName As String, Section As String, Name As String, Value As String) As Long
'设置指定键的值
SetValue = WritePrivateProfileString(Section, Name, Value, fileName)
End FunctionPublic Function EnumValue(fileName As String, Section As String, Key() As String) As Long
'获取指定节点下的所有键
On Error GoTo er
Dim ss As String
Dim pos As Integer, Count As Integer
Buffer = String(32767, 0)
GetPrivateProfileSection Section, Buffer, Len(Buffer), fileName
pos = InStr(Buffer, Chr(0))
Do
ReDim Preserve Key(Count)
ss = Left(Buffer, pos - 1)
Key(Count) = Left(ss, InStr(1, ss, "=") - 1)
Buffer = Mid(Buffer, pos + 1)
pos = InStr(Buffer, Chr(0))
If pos <= 1 Then Exit Do
Count = Count + 1
Loop
EnumValue = Count
Exit Function
er:
ReDim Key(0)
EnumValue = 0
End FunctionPublic Function EnumSection(fileName As String, Section() As String) As Long
'获取所有节点
On Error GoTo er
Dim Length As Long, pos As Integer, Count As Integer
Buffer = String(32767, 0)
Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName)
Buffer = Left(Buffer, Length)
Do
ReDim Preserve Section(Count)
pos = InStr(Buffer, Chr(0))
If Left(Buffer, pos - 1) = "" Then Exit Do
Section(Count) = Left(Buffer, pos - 1)
Buffer = Mid(Buffer, pos + 1)
If Len(Buffer) <= 0 Then Exit Do
Count = Count + 1
Loop
EnumSection = Count
Exit Function
er:
ReDim Section(0)
EnumSection = 0
End FunctionPublic Function SectionExists(fileName As String, Section As String) As Boolean
On Error GoTo er
Dim Length As Long, pos As Integer, Count As Integer
Buffer = String(32767, 0)
Length = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, Len(Buffer), fileName)
Buffer = Left(Buffer, Length)
Do
pos = InStr(Buffer, Chr(0))
If Left(Buffer, pos - 1) = "" Then Exit Do
If UCase(Left(Buffer, pos - 1)) = UCase(Section) Then SectionExists = True: Exit Do
Buffer = Mid(Buffer, pos + 1)
If Len(Buffer) <= 0 Then Exit Do
Loop
Exit Function
er:
SectionExists = False
End Function
哪个高手用此模块给个详细的例子来操作一下 新建INI、新建值、和修改、删除这些功能吧!初学啊,不知道怎么调用!