vb 读写配置文件

来源:百度知道 编辑:UC知道 时间:2024/06/23 03:59:11
'定义写入配置文件函数
Public Sub WriteToIni(ByVal Filename As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
Dim buff As String
buff = New String("", 256)
buff = Value + Chr(0)
WritePrivateProfileString(Section, Key, buff, Filename)
End Sub

'定义读取配置文件函数
Public Function ReadFROMIni(ByVal Filename As String, ByVal Section As String, ByVal Key As String) As String
Dim i As Long
Dim buff As String
buff = New String("", 256)
GetPrivateProfileString(Section, Key, "", buff, 128, Filename)
i = InStr(buff, Chr(0))
ReadFROMIni = Trim(Left(buff, i - 1))
End Function
在知道里搜的代码,谁知道怎么用?
或是给我给读写配置文件的代码,要解释

WritePrivateProfileString是写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

然后GetPrivateProfileString WritePrivateProfileString 这两个函数就可以用了.

读取和写入代码如下
Private Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)
Dim res%
res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, "C:\123.ini")
End Sub
Private Function Get