当前位置:首页 > 点滴记录 > 正文

读取多个.ini配置文件方法

读取多个.ini配置文件方法

在模块中加入下面代码 '读配置文件(调用系统库函数 Private Declare Function GetPrivateProfileString...

在模块中加入下面代码

 '读配置文件(调用系统库函数)

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 Long


Public Function GetIniParam(NomFichier As String, NomSection As String, NomVariable As String) As String

Dim ReadString As String * 255

Dim returnv As String

Dim mResultLen As Integer

mResultLen = GetPrivateProfileString(NomSection, NomVariable, "(Unassigned)", ReadString, Len(ReadString) - 1, NomFichier)

If IsNull(ReadString) Or Left$(ReadString, 12) = "(Unassigned)" Then

Dim Tempvalue As Variant

Dim Message As String

MsgBox "配置文件: " & NomFichier & " 不存在."

returnv = ""

Else

returnv = Left$(ReadString, InStr(ReadString, Chr$(0)) - 1)

End If

GetIniParam = returnv

End Function

 

Public Function WriteWinIniParam(NomDuIni As String, sLaSection As String, sNouvelleCle As String, sNouvelleValeur As String)

Dim iSucccess As Integer

iSucccess = WritePrivateProfileString(sLaSection, sNouvelleCle, sNouvelleValeur, NomDuIni)

If iSucccess = 0 Then

WriteWinIniParam = False

Else

WriteWinIniParam = True

End If

End Function


然后在窗体代码内调用,就像下面这样

image.png

Private Sub Command1_Click()

Call WriteWinIniParam(App.Path & "\sys.ini", "系统配置", "数据库路径", Text1.Text)

Text1.Text = GetIniParam(App.Path & "\sys.ini", "系统配置", "程序名")

End Sub

这样就ok了

发表评论

最新文章

取消
扫码支持 支付码