VB.NET读写INI文件

 

    'ini文件操作

    '声明API

    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32

    '获取ini文件信息
    Private Function GetIni(ByVal _cStr As String, ByVal _iStr As String) As String

        Dim strIni As String
        strIni = New String("", 101)
        Dim oStr As String = ""

        GetPrivateProfileString(_cStr, _iStr, "", strIni, 100, Application.StartupPath + "/Send.ini")
        oStr = Microsoft.VisualBasic.Left(strIni, InStr(strIni, Chr(0)) - 1)

        Return oStr

    End Function

    '设置ini文件信息
    Private Sub SetIni(ByVal _cStr As String, ByVal _iStr As String, ByVal _iSet As String)

        If _cStr = "" Then
            Exit Sub
        End If
        If _iStr = "" Then
            Exit Sub
        End If

        WritePrivateProfileString(_cStr, _iStr, _iSet, Application.StartupPath + "/Send.ini")

    End Sub

 

 

'调用

读出

   MessageBox.Show(GetIni("MSSQL", "Server"))

写入

SetIni("MSSQL", "Server", Me.TextBox1.Text)

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页