'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '%% 注意下面被注释的三个声明语句与新的声明有何不同 %% '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lIniFileName As String) As Long Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As Any, ByVal lIniFileName As String) As Long 'Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lIniFileName 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 lIniFileName 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 lIniFileName As String) As Long Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As Any, ByVal nSize As Long, ByVal lIniFileName 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 lIniFileName As String) As Long'******************************************* ' 从*.ini文件中读一个条目 '******************************************* Public Function INIRead(Section As String, Key As String, IniFile As String) As String Dim x As Long, Buff As String Buff = String(255, " ") x = GetPrivateProfileString(Section, Key, "", Buff, 255, IniFile)
If x = 0 Then INIRead = "" Else INIRead = Trim$(Left(Buff, InStr(Buff, Chr$(0)) - 1)) End If End Function '******************************************* ' 从*.ini文件中写一个条目/新建 '******************************************* Public Function INIWrite(Section As String, Key As String, Value As String, IniFile As String) As String WritePrivateProfileString Section, Key, Value, IniFile End Function
INIWrite一样的 (Section As String, Key As String, Value As String, IniFile As String) As String 使用:(Value 是键值,就是上面的"abc.exe")直接调用 INIWrite "[Win]","run","abc.exe","c:\abc.ini")
给你个我以前写好的函数,读c:\setup.ini里的[setup information]下的server name就这么写: aa = GetProfile("c:\setup.ini", "setup information", "server name") Function GetProfile(strFileName As String, strSection As String, strName As String) As String
'this function use for read file of ini 'strFileName file name for read 'strSection node of file 'strName field name of file strSectionTemp = "" strNameTemp = "" strreturn = "" On Error GoTo ErrReadFile Open strFileName For Input As #1
'find node
Do While Not EOF(1) strCharA = Input(1, #1) If strCharA = "[" Then Do While Not EOF(1) strCharB = Input(1, #1) If strCharB = "]" Then Exit Do strSectionTemp = strSectionTemp & strCharB Loop End If If strSectionTemp = strSection Then strCharA = Input(2, #1) Exit Do Else strSectionTemp = "" End If Loop On Error GoTo ErrSrchSection
aa: 'find field
strNameTemp = "" Do While Not EOF(1) strCharA = Input(1, #1) If strCharA <> "=" Then strNameTemp = strNameTemp & strCharA 'gained name Else Exit Do End If Loop If strNameTemp = strName Then Line Input #1, strreturn Else Line Input #1, strreturn GoTo aa End If Close #1 GetProfile = strreturn Exit Function ErrReadFile: Dim inrRet As Integer intret = MsgBox("read 'test.ini' failed!" & Chr(10) & "a file is missing or invalid", vbAbortRetryIgnore + vbExclamation, "error") Select Case intret Case vbAbort GetProfile = "" Close #1 End Exit Function Case vbRetry Resume Case vbIgnore Resume Next End Select ErrSrchSection: MsgBox "no node in 'ini' file", vbOKOnly + vbExclamation, "error" GetProfile = "" Close #1 End End Function
Public 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) '读敢INI APIPublic Declare Function WritePrivateProfileString& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String) '写入INI API'读取 Configini = App.Path + "\Config.ini" '程序配置文件路径 Configini = Replace(Configini, "\\", "\") If Dir(Configini) <> "" Then GetPrivateProfileString "ComSet", "BandRate", "", TmpIniStr, 255, Configini If Left$(TmpIniStr, 1) <> Chr$(0) Then ComboBandRate.Text = Left$(TmpIniStr, InStr(1, TmpIniStr, Chr$(0))) end if'写入 WritePrivateProfileString "ComSet", "BandRate", ComboBandRate.Text, Configini
啊,少了两句,加上去就行了Public Configini As String 'INI路径 Public TmpIniStr As String * 255
GetPrivateProfileInt
GetPrivateProfileSection
GetPrivateProfileSectionNames
GetPrivateProfileString
GetPrivateProfileStruct
GetProfileInt
GetProfileSection
GetProfileString
WritePrivateProfileSection
WritePrivateProfileString
WritePrivateProfileStruct
WriteProfileSection
WriteProfileString
用instr 找到 <script ,然后把str=......提取出来。
'%% 注意下面被注释的三个声明语句与新的声明有何不同 %%
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lIniFileName As String) As Long
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As Any, ByVal lIniFileName As String) As Long
'Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lIniFileName 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 lIniFileName 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 lIniFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As Any, ByVal nSize As Long, ByVal lIniFileName 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 lIniFileName As String) As Long'*******************************************
' 从*.ini文件中读一个条目
'*******************************************
Public Function INIRead(Section As String, Key As String, IniFile As String) As String
Dim x As Long, Buff As String
Buff = String(255, " ")
x = GetPrivateProfileString(Section, Key, "", Buff, 255, IniFile)
If x = 0 Then
INIRead = ""
Else
INIRead = Trim$(Left(Buff, InStr(Buff, Chr$(0)) - 1))
End If
End Function
'*******************************************
' 从*.ini文件中写一个条目/新建
'*******************************************
Public Function INIWrite(Section As String, Key As String, Value As String, IniFile As String) As String
WritePrivateProfileString Section, Key, Value, IniFile
End Function
然后在你需要的地方加入:
dim tmpstr as string
'参数Section, Key , IniFile
'Section:小节的名称
'Key :键的名称
'IniFile:要读的文件名
'例如有个ini文件在c:\下,内容如下
'[Win]
'run=abc.exe
'其中[Win]是小节的名称,run是键的名称tmpstr = INIRead("[Win]","run","c:\abc.ini")
最后tmpstr 就是"abc.exe"
(Section As String, Key As String, Value As String, IniFile As String) As String
使用:(Value 是键值,就是上面的"abc.exe")直接调用 INIWrite "[Win]","run","abc.exe","c:\abc.ini")
aa = GetProfile("c:\setup.ini", "setup information", "server name")
Function GetProfile(strFileName As String, strSection As String, strName As String) As String
'this function use for read file of ini
'strFileName file name for read
'strSection node of file
'strName field name of file strSectionTemp = ""
strNameTemp = ""
strreturn = ""
On Error GoTo ErrReadFile
Open strFileName For Input As #1
'find node
Do While Not EOF(1)
strCharA = Input(1, #1)
If strCharA = "[" Then
Do While Not EOF(1)
strCharB = Input(1, #1)
If strCharB = "]" Then Exit Do
strSectionTemp = strSectionTemp & strCharB
Loop
End If
If strSectionTemp = strSection Then
strCharA = Input(2, #1)
Exit Do
Else
strSectionTemp = ""
End If
Loop
On Error GoTo ErrSrchSection
aa: 'find field
strNameTemp = ""
Do While Not EOF(1)
strCharA = Input(1, #1)
If strCharA <> "=" Then
strNameTemp = strNameTemp & strCharA 'gained name
Else
Exit Do
End If
Loop
If strNameTemp = strName Then
Line Input #1, strreturn
Else
Line Input #1, strreturn
GoTo aa
End If
Close #1
GetProfile = strreturn
Exit Function
ErrReadFile:
Dim inrRet As Integer
intret = MsgBox("read 'test.ini' failed!" & Chr(10) & "a file is missing or invalid", vbAbortRetryIgnore + vbExclamation, "error")
Select Case intret
Case vbAbort
GetProfile = ""
Close #1
End
Exit Function
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
ErrSrchSection:
MsgBox "no node in 'ini' file", vbOKOnly + vbExclamation, "error"
GetProfile = ""
Close #1
End
End Function
Configini = App.Path + "\Config.ini" '程序配置文件路径
Configini = Replace(Configini, "\\", "\")
If Dir(Configini) <> "" Then
GetPrivateProfileString "ComSet", "BandRate", "", TmpIniStr, 255, Configini
If Left$(TmpIniStr, 1) <> Chr$(0) Then ComboBandRate.Text = Left$(TmpIniStr, InStr(1, TmpIniStr, Chr$(0)))
end if'写入
WritePrivateProfileString "ComSet", "BandRate", ComboBandRate.Text, Configini
Public TmpIniStr As String * 255