Visual basic 6读写ini文件
ini文件是windows的系统配置文件, 被用来对操作系统或特定程序初始化或进行参数设置. 在Windows系统中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。
中间的数据格式一般为:
;注释(Comments)
[Section1 Name]
KeyName1=value1
KeyName2=value2
...
[Section2 Name]
KeyName1=value1
KeyName2=value2
ini 文件可以分为几个 Section,每个 Section 的名称用 [] 括起来,在一个 Section 中,可以有很多的 Key,每一个 Key 可以有一个值并占用一行,格式是 Key=value,注释以分号";"开头。
Windows提供了几个有用的API来读写操作INI文件:
GetPrivateProfileSection - 从 ini 文件中读出整个 Section 的内容
WritePrivateProfileSection - 将一个整个 Section 的内容入 ini 文件的指定 Section 中
WritePrivateProfileString - 将一个 Key 值写入 ini 文件的指定 Section 中
'declarations for working with Ini files
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
'reads an Ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255
Dim v As Long
v = GetPrivateProfileString(Section, Key, "NotFound", RetVal, 255, Filename)
ReadIni = Left(RetVal, v)
End Function
'reads an Ini sectionPublic
Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255
Dim v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v )
End Function
'writes an Ini string
Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub
'writes an Ini section
Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
WritePrivateProfileSection Section, Value, Filename
End Sub
-----------------------------------------------------------
佛对我说:你心里有尘。我用力的拭擦。