函数'两个函数 , 先在一个模快中定义API函数
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
'如果是读INT值可以用字符串转化,所以没有另外定义函数
'Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPriviteProfileIntA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'定义读与写INI文件的函数
'****读INI文件****
'文件名 lpFileName 如果不存在会自己创建,如果只有文件名,默认在Windows\system目录下
'[lpAppName]
'lpKeyName=取回的设置值
'lpDefault 当键值不存在时的默认值
Public Function ReadINI(lpFileName As String, lpAppName As String, LpKeyName As String) As String
Dim Temp As String * 20
Dim lpDefault As String
lpDefault = ""
If GetPrivateProfileString(lpAppName, LpKeyName, lpDefault, Temp, Len(Temp), lpFileName) <= 0 Then
ReadINI = ""
Else
ReadINI = MyTrim(Temp) 'MyTrim函数见下
End If
End Function
'****写INI文件****
'[lpAppName]
'lpKeyName=lpString
Public Function WriteINI(lpFileName As String, lpAppName As String, LpKeyName As String, lpString As String) As Boolean
If WritePrivateProfileString(lpAppName, LpKeyName, lpString, lpFileName) = 0 Then
WriteINI = False
Else
WriteINI = True
End If
End Function'包含三个函数,分别取Rtrim,Ltrim,Trim
'可以去字符串中如ASC码为0,10,13,32的字符
Public Function MyRtrim(Tmpstr As String)
Dim i, s As Integer
i = Len(Tmpstr)
If i = 0 Then
MyRtrim = ""
Exit Function
End If
s = Asc(Right(Tmpstr, 1))
While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
i = i - 1
Tmpstr = Left(Tmpstr, i)
If Len(Tmpstr) = 0 Then
MyRtrim = ""
Exit Function
End If
s = Asc(Right(Tmpstr, 1))
Wend
MyRtrim = Tmpstr
End FunctionPublic Function MyLtrim(Tmpstr As String)
Dim i, s As Integer
i = Len(Tmpstr)
If i = 0 Then
MyLtrim = ""
Exit Function
End If
s = Asc(Left(Tmpstr, 1))
While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
i = i - 1
Tmpstr = Right(Tmpstr, i)
If Len(Tmpstr) = 0 Then
MyLtrim = Tmpstr
Exit Function
End If
s = Asc(Left(Tmpstr, 1))
Wend
MyLtrim = Tmpstr
End FunctionPublic Function MyTrim(Tmpstr As String)
Tmpstr = MyLtrim(Tmpstr)
Tmpstr = MyRtrim(Tmpstr)
MyTrim = Tmpstr
End Function调用:
DBServer = ReadINI("Manager.ini", "", "DBServerName")
DBName = ReadINI("Manager.ini", "", "DBName")
DBUser = ReadINI("Manager.ini", "", "DBUser")
DBPwd = ReadINI("Manager.ini", "", "DBPwd")用绝对路径也不行,为什么阿再线等
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
'如果是读INT值可以用字符串转化,所以没有另外定义函数
'Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPriviteProfileIntA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'定义读与写INI文件的函数
'****读INI文件****
'文件名 lpFileName 如果不存在会自己创建,如果只有文件名,默认在Windows\system目录下
'[lpAppName]
'lpKeyName=取回的设置值
'lpDefault 当键值不存在时的默认值
Public Function ReadINI(lpFileName As String, lpAppName As String, LpKeyName As String) As String
Dim Temp As String * 20
Dim lpDefault As String
lpDefault = ""
If GetPrivateProfileString(lpAppName, LpKeyName, lpDefault, Temp, Len(Temp), lpFileName) <= 0 Then
ReadINI = ""
Else
ReadINI = MyTrim(Temp) 'MyTrim函数见下
End If
End Function
'****写INI文件****
'[lpAppName]
'lpKeyName=lpString
Public Function WriteINI(lpFileName As String, lpAppName As String, LpKeyName As String, lpString As String) As Boolean
If WritePrivateProfileString(lpAppName, LpKeyName, lpString, lpFileName) = 0 Then
WriteINI = False
Else
WriteINI = True
End If
End Function'包含三个函数,分别取Rtrim,Ltrim,Trim
'可以去字符串中如ASC码为0,10,13,32的字符
Public Function MyRtrim(Tmpstr As String)
Dim i, s As Integer
i = Len(Tmpstr)
If i = 0 Then
MyRtrim = ""
Exit Function
End If
s = Asc(Right(Tmpstr, 1))
While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
i = i - 1
Tmpstr = Left(Tmpstr, i)
If Len(Tmpstr) = 0 Then
MyRtrim = ""
Exit Function
End If
s = Asc(Right(Tmpstr, 1))
Wend
MyRtrim = Tmpstr
End FunctionPublic Function MyLtrim(Tmpstr As String)
Dim i, s As Integer
i = Len(Tmpstr)
If i = 0 Then
MyLtrim = ""
Exit Function
End If
s = Asc(Left(Tmpstr, 1))
While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
i = i - 1
Tmpstr = Right(Tmpstr, i)
If Len(Tmpstr) = 0 Then
MyLtrim = Tmpstr
Exit Function
End If
s = Asc(Left(Tmpstr, 1))
Wend
MyLtrim = Tmpstr
End FunctionPublic Function MyTrim(Tmpstr As String)
Tmpstr = MyLtrim(Tmpstr)
Tmpstr = MyRtrim(Tmpstr)
MyTrim = Tmpstr
End Function调用:
DBServer = ReadINI("Manager.ini", "", "DBServerName")
DBName = ReadINI("Manager.ini", "", "DBName")
DBUser = ReadINI("Manager.ini", "", "DBUser")
DBPwd = ReadINI("Manager.ini", "", "DBPwd")用绝对路径也不行,为什么阿再线等
DBServer = ReadINI("c:\seari\Manager.ini", "", "DBServerName")
DBName = ReadINI("c:\seari\Manager.ini", "", "DBName")
DBUser = ReadINI("c:\seari\Manager.ini", "", "DBUser")
DBPwd = ReadINI("c:\seari\Manager.ini", "", "DBPwd")
这样也不行啊
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
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
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'设置或读取INI文件数据
'---------------------(1)-------------------------------
Function ReadWriteINI(FileName As String, Mode As String, tmpSecname As String, tmpKeyname As String, Optional tmpKeyValue) As String
Dim tmpString As String
Dim secname As String
Dim keyname As String
Dim keyvalue As String
Dim anInt
Dim defaultkey As String
On Error GoTo ReadWriteINIError
'
' *** set the return value to OK
'ReadWriteINI = "OK"
' *** test for good data to work with
If IsNull(Mode) Or Len(Mode) = 0 Then
ReadWriteINI = "ERROR MODE" ' Set the return value
Exit Function
End If
If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then
ReadWriteINI = "ERROR Secname" ' Set the return value
Exit Function
End If
If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then
ReadWriteINI = "ERROR Keyname" ' Set the return value
Exit Function
End If
' *** set the ini file name
'FileName = "C:\sss.ini" ' <<<<< put your file name here
'
'
' ******* WRITE MODE *************************************
If UCase(Mode) = "WRITE" Then
If IsNull(tmpKeyValue) Or Len(tmpKeyValue) = 0 Then
ReadWriteINI = "ERROR KeyValue"
Exit Function
Else
secname = tmpSecname
keyname = tmpKeyname
keyvalue = tmpKeyValue
anInt = WritePrivateProfileString(secname, keyname, keyvalue, FileName)
End If
End If
' *******************************************************
'
' ******* READ MODE *************************************
If UCase(Mode) = "GET" Then
secname = tmpSecname
keyname = tmpKeyname
defaultkey = "Failed"
keyvalue = String$(50, 32)
anInt = GetPrivateProfileString(secname, keyname, defaultkey, keyvalue, Len(keyvalue), FileName)
If Left(keyvalue, 6) <> "Failed" Then ' *** got it
tmpString = keyvalue
tmpString = RTrim(tmpString)
tmpString = Left(tmpString, Len(tmpString) - 1)
End If
ReadWriteINI = tmpString
End If
Exit Function
' *******
ReadWriteINIError:
MsgBox Error
Stop
End Function