2021年10月13日 星期三

[VB] 讀取INI檔

把以下CLASS貼上後可直接使用
讀檔ReadINI(File, Section, Key)
寫入WriteINI(File, Section, Key, Value)

Imports System.IO

Imports System.Text

Imports System.Runtime.InteropServices

Class INIReadWrite

    <DllImport("kernel32.dll", SetLastError:=True)>

    Private Shared Function

        GetPrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal

        lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer

    End Function

    <DllImport("kernel32.dll", SetLastError:=True)>

    Private Shared Function         WritePrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean

    End Function

    Public Shared Function ReadINI(ByVal File As String, ByVal Section As String, ByVal Key As String) As String

        Dim sb As New         StringBuilder(500)

        GetPrivateProfileString(Section, Key, "", sb,sb.Capacity, File)

        Return sb.ToString

    End Function

    Public Shared Sub WriteINI(ByVal File As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)

        WritePrivateProfileString(Section, Key,Value, File)

    End Sub

End Class

沒有留言:

張貼留言