ReadINI.bas: Option Explicit 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'对INI文件进行读操作,其中:参数一:文件路径,参数二:条目的小节名称,参数三:项名或条目名 Public Function ReadINI(StrFileName As String, StrAppName As String, StrKeyName As String) As String ReadINI = String(255, 0) GetPrivateProfileString StrAppName, StrKeyName, "", ReadINI, 255, StrFileName ReadINI = Left(ReadINI, InStr(ReadINI, Chr(0)) - 1) End Function程序中: Dim FileRoot As StringFileRoot = "ini文件的路径" label1.caption = ReadINI(FileRoot, "小节名", "变量名") & vbCrLfini文件格式: [小节名] 变量名=内容
'require variable declaration Option Explicit'declares for ini controlling 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 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 WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, 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 Long'when form is loaded Private Sub Form_Load()'if error occures resume still On Error Resume Next'local variables Dim File As String, OFLen As Double, _ Str As String'set our varibles File = "C:\temp.txt" OFLen = FileLen(File)'write few example sections: WriteIniSection File, "Test1", "" WriteIniSection File, "Test2", "Here shoud be found some text"'write few ini strings WriteIni File, "Test3", "Ini1", "This is ini 1" WriteIni File, "Test1", "Ini2", "This is ini 2"'inform we're written the data MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)'read the ini file Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf'show data MsgBox Str'end application EndEnd Sub'// INI CONTROLLING PROCEDURES'reads ini string Public Function ReadIni(Filename As String, Section As String, Key As String) As String Dim RetVal As String * 255, v As Long v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename) ReadIni = Left(RetVal, v - 1) End Function'reads ini section Public Function ReadIniSection(Filename As String, Section As String) As String Dim RetVal As String * 255, v As Long v = GetPrivateProfileSection(Section, RetVal, 255, Filename) ReadIniSection = Left(RetVal, v - 1) End Function'writes ini Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String) WritePrivateProfileString Section, Key, Value, Filename End Sub'writes ini section Public Sub WriteIniSection(Filename As String, Section As String, Value As String) WritePrivateProfileSection Section, Value, Filename End Sub
建立与读取.ini文件
'请於form中放3个TextBox,一个CommandBox 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 Sub Command1_Click() Dim success As Long success = WritePrivateProfileString("MyApp", "text1", Text1.Text, "c:\aa.ini") '叁数一 Section Name '叁数二 於.ini中的项目 '叁数三 项目的内容 '叁数四 .ini文件的名称 success = WritePrivateProfileString("MyApp", "text2", Text2.Text, "c:\aa.ini") success = WritePrivateProfileString("MyApp2", "text3", Text3.Text, "c:\aa.ini") End SubPrivate Sub Form_load() Dim ret As Long Dim buff As String buff = String(255, 0) ret = GetPrivateProfileString("Myapp", "text1", "text1", buff, 256, "c:\aa.ini") '若.ini MyApp中无text1,则采用叁数三的值 Text1.Text = buff buff = String(255, 0) ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini") Text2.Text = buff buff = String(255, 0) ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini") Text3.Text = buff End Sub
If Dir(appPath & "aaa.ini") = "" Then MsgBox "配置文件" & appPath & "aaa.ini未找到,程序终止!" End End If 一下是INI文件中的内容: servername=(local) databasename=abc provider=SQLOLEDB uid=sa
Option Explicit
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'对INI文件进行读操作,其中:参数一:文件路径,参数二:条目的小节名称,参数三:项名或条目名
Public Function ReadINI(StrFileName As String, StrAppName As String, StrKeyName As String) As String
ReadINI = String(255, 0)
GetPrivateProfileString StrAppName, StrKeyName, "", ReadINI, 255, StrFileName
ReadINI = Left(ReadINI, InStr(ReadINI, Chr(0)) - 1)
End Function程序中:
Dim FileRoot As StringFileRoot = "ini文件的路径"
label1.caption = ReadINI(FileRoot, "小节名", "变量名") & vbCrLfini文件格式:
[小节名]
变量名=内容
'require variable declaration
Option Explicit'declares for ini controlling
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 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 WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, 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 Long'when form is loaded
Private Sub Form_Load()'if error occures resume still
On Error Resume Next'local variables
Dim File As String, OFLen As Double, _
Str As String'set our varibles
File = "C:\temp.txt"
OFLen = FileLen(File)'write few example sections:
WriteIniSection File, "Test1", ""
WriteIniSection File, "Test2", "Here shoud be found some text"'write few ini strings
WriteIni File, "Test3", "Ini1", "This is ini 1"
WriteIni File, "Test1", "Ini2", "This is ini 2"'inform we're written the data
MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)'read the ini file
Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf'show data
MsgBox Str'end application
EndEnd Sub'// INI CONTROLLING PROCEDURES'reads ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v - 1)
End Function'reads ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v - 1)
End Function'writes ini
Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub'writes ini section
Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
WritePrivateProfileSection Section, Value, Filename
End Sub
建立与读取.ini文件
'请於form中放3个TextBox,一个CommandBox
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 Sub Command1_Click()
Dim success As Long
success = WritePrivateProfileString("MyApp", "text1", Text1.Text, "c:\aa.ini")
'叁数一 Section Name
'叁数二 於.ini中的项目
'叁数三 项目的内容
'叁数四 .ini文件的名称
success = WritePrivateProfileString("MyApp", "text2", Text2.Text, "c:\aa.ini")
success = WritePrivateProfileString("MyApp2", "text3", Text3.Text, "c:\aa.ini")
End SubPrivate Sub Form_load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text1", "text1", buff, 256, "c:\aa.ini")
'若.ini MyApp中无text1,则采用叁数三的值
Text1.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini")
Text2.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini")
Text3.Text = buff
End Sub
MsgBox "配置文件" & appPath & "aaa.ini未找到,程序终止!"
End
End If
一下是INI文件中的内容:
servername=(local)
databasename=abc
provider=SQLOLEDB
uid=sa