vb 操作 ini 的类

cIniFile - 类模块

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 GetPrivateProfileInt Lib "kernel32" _
    Alias "GetPrivateProfileIntA" ( _
    ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, _
    ByVal nDefault 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

   Private IniFileName As String
   Public ErrorMsg As String
Private Sub Class_Initialize()
    IniFileName = vbNullString
    ErrorMsg = vbNullString
End Sub

Public Sub SpecifyIni(FilePathName)
    IniFileName = Trim(FilePathName)
End Sub
Private Function NoIniFile() As Boolean
    NoIniFile = True
    If IniFileName = vbNullString Then
       ErrorMsg = "No INI file"
        Exit Function
    End If
    ErrorMsg = vbNullString
    NoIniFile = False
End Function
Public Function WriteString(Section As String, key As String, Value As _
String) As Boolean
    WriteString = False
    If NoIniFile() Then
        Exit Function
    End If
    If WritePrivateProfileString(Section, key, Value, IniFileName) = 0 Then
        ErrorMsg = "Write failure"
        Exit Function
    End If
    WriteString = True
End Function

Public Function ReadString(Section As String, key As String, Size As _
Long) As String
    Dim ReturnStr As String
    Dim ReturnLng As Long
    ReadString = vbNullString
    If NoIniFile() Then
        Exit Function
    End If
    ReturnStr = Space(Size)
    ReturnLng = GetPrivateProfileString(Section, key, vbNullString, _
ReturnStr, Size, IniFileName)
    ReadString = Left(ReturnStr, ReturnLng)
End Function
Public Function ReadInt(Section As String, key As String) As Long
    Dim ReturnLng As Long
    ReadInt = 0
    ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)
    If ReturnLng = 0 Then
        ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName)
        If ReturnLng = 1 Then
            ErrorMsg = "Cannot read INT"
            Exit Function
        End If
    End If
    ReadInt = ReturnLng
End Function

窗口,添加 两个按钮

Private Sub Command1_Click()
    Dim GetIniString As New cIniFile

    GetIniString.SpecifyIni (App.Path & "" & "Config.ini")
    MsgBox GetIniString.ReadString("Setting", "ServerIP", 128)
    Set GetIniString = Nothing
   
End Sub

Private Sub Command2_Click()
    Dim WriteIniString As New cIniFile
   
    WriteIniString.SpecifyIni (App.Path & "" & "Config.ini")
    MsgBox WriteIniString.WriteString("Setting", "ServerIP", "192.168.1.200")
    Set WriteIniString = Nothing
   
End Sub

Config.ini

[Setting]
ServerIP=192.168.1.253

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据