'建立与读取.ini文件 'API函数声明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 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 '写入ini Public Sub SaveSettings() Dim SaveSettings As Long '参数一 项目的标题 '参数二 .ini中的项目 '参数三 项目的内容,必须为字符串类型(string)! '参数四 .ini文件的名称 SaveSettings = WritePrivateProfileString("Settings", "Check1Value", text1.text,App.Path + "\data.ini")'在程序的目录下的data.ini把text1.text的内容写入到项目的标题为Settings,项目为Check1Value下 SaveSettings = WritePrivateProfileString("Settings", "Check2Value", text2.text, App.Path + "\data.ini") end sub '读入ini Public Sub LoadSettings() Dim LoadSettings As Long Dim buff As String
'INI 文件读写 Option Explicit' API to read/write ini's 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 Integer, ByVal lpFileName As String) As IntegerPublic Function SaveKeyValue(ByVal Section$, ByVal Key$, ByVal Value$, ByRef File$) Dim retval As Integer SaveKeyValue = WritePrivateProfileString(Section$, Key$, Value$, File$) End Function'返回 Section 中指定 Key 的 Value Public Function GetKeyValue(ByVal Section As String, ByVal Key As String, ByRef File$) As String Dim retval As Integer Dim t As String * 255 ' Get the value retval = GetPrivateProfileString(Section, Key, "unknown value", t, Len(t), File$) ' If there is one, return it If retval > 0 Then GetKeyValue = Left$(t, retval) Else GetKeyValue = "Unknown section or key" End IfEnd Function'获得指定 Section 中所有的 KeyValue 存放在 KeyArray(), 返回 KeyArray() 的个数 Public Function GetSection(ByVal Section As String, KeyArray() As String, ByRef File$) As Integer Dim 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
' 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 ReDim KeyArray(retval / 3 - 1, 1) 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 KeyArray(ArrayCount, 0) = keystring KeyArray(ArrayCount, 1) = GetKeyValue(Section, keystring, File$) ' ' Find next null lastpointer = nullpointer + 1 nullpointer = InStr(nullpointer + 1, t, Chr$(0)) ArrayCount = ArrayCount + 1 Loop End If ' ' Return the number of array elements GetSection = ArrayCount
End Function循环查找KeyArray(.,1)的值找 "111"
Dim str as StringOpen CommonDialog1.Name For Input As #1 ReDim str() Do While not EOF(1) Line Input #1,Line str = split(Line,"=") If str(1) = "111" Then Text1.text = str(0) End If Loop Close #1-------try pls
只能同过打开文件按行读取,进行字符比较 然后截取字符串的方法。
'API函数声明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
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
'写入ini
Public Sub SaveSettings()
Dim SaveSettings As Long
'参数一 项目的标题
'参数二 .ini中的项目
'参数三 项目的内容,必须为字符串类型(string)!
'参数四 .ini文件的名称
SaveSettings = WritePrivateProfileString("Settings", "Check1Value", text1.text,App.Path + "\data.ini")'在程序的目录下的data.ini把text1.text的内容写入到项目的标题为Settings,项目为Check1Value下 SaveSettings = WritePrivateProfileString("Settings", "Check2Value", text2.text, App.Path + "\data.ini")
end sub
'读入ini
Public Sub LoadSettings()
Dim LoadSettings As Long
Dim buff As String
'若.ini MyApp中无text1,则采用参数三的值
buff = String(255, 0)
LoadSettings = GetPrivateProfileString("Settings", "Check1Value", "1", buff, 256, App.Path + "\data.ini")
text1.text= buff '同理,把Check1Value的内容付给text1.text
buff = String(255, 0)
LoadSettings = GetPrivateProfileString("Settings", "Check2Value", "1", buff, 256, App.Path + "\data.ini")
text2.text= buff
end sub
[Settings]
Check1Value=text1.text的内容
Check2Value=text2.text的内容
Option Explicit' API to read/write ini's
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 Integer, ByVal lpFileName As String) As IntegerPublic Function SaveKeyValue(ByVal Section$, ByVal Key$, ByVal Value$, ByRef File$)
Dim retval As Integer
SaveKeyValue = WritePrivateProfileString(Section$, Key$, Value$, File$)
End Function'返回 Section 中指定 Key 的 Value
Public Function GetKeyValue(ByVal Section As String, ByVal Key As String, ByRef File$) As String
Dim retval As Integer
Dim t As String * 255 ' Get the value
retval = GetPrivateProfileString(Section, Key, "unknown value", t, Len(t), File$) ' If there is one, return it
If retval > 0 Then
GetKeyValue = Left$(t, retval)
Else
GetKeyValue = "Unknown section or key"
End IfEnd Function'获得指定 Section 中所有的 KeyValue 存放在 KeyArray(), 返回 KeyArray() 的个数
Public Function GetSection(ByVal Section As String, KeyArray() As String, ByRef File$) As Integer Dim 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
' 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
ReDim KeyArray(retval / 3 - 1, 1)
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
KeyArray(ArrayCount, 0) = keystring
KeyArray(ArrayCount, 1) = GetKeyValue(Section, keystring, File$)
'
' Find next null
lastpointer = nullpointer + 1
nullpointer = InStr(nullpointer + 1, t, Chr$(0))
ArrayCount = ArrayCount + 1
Loop
End If
'
' Return the number of array elements
GetSection = ArrayCount
End Function循环查找KeyArray(.,1)的值找 "111"
ReDim str()
Do While not EOF(1)
Line Input #1,Line
str = split(Line,"=")
If str(1) = "111" Then
Text1.text = str(0)
End If
Loop
Close #1-------try pls