新人求助一个读取INI的问题
SYSTEM_Setup.INI文件的内容
[服务器配置]
IP=NYCL-PC
Port=hxdata
UserName=sa
Password=123456
希望在VB6的服务器连接模块中定义一个变量,让这个变量去读取INI中,[服务器配置]下面IP=右面的内容,以方便让代码调用这个变量去连接服务器,请教一个各位老师,这个代码该怎么去写?
[解决办法]
'读写ini文件
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文件,不固定节点,in_key为写入/读取的主键
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String, iniPath As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, iniPath
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String, iniPath As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, iniPath
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function
这是读写ini文件的模块
GetIniStr("服务器配置", "IP", App.Path & "\SYSTEM_Setup.INI")
WriteIniStr("服务器配置", "IP", "NYCL-PC", App.Path & "\SYSTEM_Setup.INI")