Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text
Namespace TcINIFile
    Public Class INIFile
        Private _FileName As String
        Private Declare Function GetPrivateProfileStringA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal keyName As String, ByVal sDefault As String, ByVal buffer As StringBuilder, ByVal nSize As Integer, ByVal fileName As String) As Integer
        Private Declare Function GetPrivateProfileSectionA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal buffer As StringBuilder, ByVal nSize As Integer, ByVal fileName As String) As Integer
        Private Declare Function WritePrivateProfileSectionA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal sValue As String, ByVal fileName As String) As Integer
        Private Declare Function WritePrivateProfileStringA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal keyName As String, ByVal sValue As String, ByVal fileName As String) As Integer
        Private Declare Function GetPrivateProfileSectionNamesA Lib "kernel32.dll" _
            (ByVal buffer As Byte(), ByVal iLen As Integer, ByVal fileName As String) As Integer
        Private Declare Function GetPrivateProfileString Lib "kernel32" _
            (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As Byte(), ByVal nSize As UInteger, ByVal lpFileName As String) As UInteger
        Public Sub New(ByVal FileName As String)
            Me._FileName = FileName
            If Not Me.FileExists() Then
                Me.CreateFile()
            End If
        End Sub
        Public Function ReadString(ByVal Section As String, ByVal Key As String) As String
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileStringA(Section, Key, "", stringBuilder, stringBuilder.Capacity, Me._FileName)
            Return stringBuilder.ToString()
        End Function
        Public Function ReadList(ByVal Section As String, ByVal Key As String) As List(Of String)
            Dim list As List(Of String) = New List(Of String)()
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileStringA(Section, Key, "", stringBuilder, stringBuilder.Capacity, Me._FileName)
            For i As Integer = 0 To stringBuilder.ToString().Split(",").Length - 1
                list.Add(stringBuilder.ToString().Split(",")(i).ToString())
            Next
            Return list
        End Function
        Public Function ReadList2(ByVal Section As String, ByVal Key As String) As List(Of String)
            Dim list As List(Of String) = New List(Of String)()
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileStringA(Section, Key, "", stringBuilder, stringBuilder.Capacity, Me._FileName)
            For i As Integer = 0 To stringBuilder.ToString().Split(vbLf).Length - 1
                list.Add(stringBuilder.ToString().Split(vbLf)(i).ToString())
            Next
            Return list
        End Function
        Public Function ReadSingleSectionKeys(ByVal Section As String) As List(Of String)
            Dim list As List(Of String) = New List(Of String)()
            Dim array As Byte() = New Byte(65536 - 1) {}
            Dim privateProfileString As UInteger = INIFile.GetPrivateProfileString(Section, Nothing, Nothing, array, CUInt(array.Length), Me._FileName)
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            While CLng(num2) < CLng((CULng(privateProfileString)))
                If array(num2) = 0 Then
                    list.Add(Encoding.[Default].GetString(array, num, num2 - num))
                    num = num2 + 1
                End If
                num2 += 1
            End While
            Return list
        End Function
        Public Overridable Function ReadInt(ByVal Section As String, ByVal Key As String) As Integer
            Dim result As Integer
            Try
                result = Integer.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1
            End Try
            Return result
        End Function
        Public Overridable Function ReadLong(ByVal Section As String, ByVal Key As String) As Long
            Dim result As Long
            Try
                result = Long.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1L
            End Try
            Return result
        End Function
        Public Overridable Function ReadByte(ByVal Section As String, ByVal Key As String) As Byte
            Dim result As Byte
            Try
                result = Byte.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = 0
            End Try
            Return result
        End Function
        Public Overridable Function ReadFloat(ByVal Section As String, ByVal Key As String) As Single
            Dim result As Single
            Try
                result = Single.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1.0F
            End Try
            Return result
        End Function
        Public Overridable Function ReadDouble(ByVal Section As String, ByVal Key As String) As Double
            Dim result As Double
            Try
                result = Double.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1.0
            End Try
            Return result
        End Function
        Public Overridable Function ReadDateTime(ByVal Section As String, ByVal Key As String) As DateTime
            Dim result As DateTime
            Try
                result = DateTime.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = DateTime.Parse("0-0-0")
            End Try
            Return result
        End Function
        Public Overridable Function ReadBool(ByVal Section As String, ByVal Key As String) As Boolean
            Dim result As Boolean
            Try
                result = Boolean.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = Boolean.Parse("0-0-0")
            End Try
            Return result
        End Function
        Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Object)
            If Value IsNot Nothing Then
                INIFile.WritePrivateProfileStringA(Section, Key, Value.ToString(), Me._FileName)
            Else
                INIFile.WritePrivateProfileStringA(Section, Key, Nothing, Me._FileName)
            End If
        End Sub
        Public Function ReadSections() As ArrayList
            Dim array As Byte() = New Byte(65535 - 1) {}
            Dim privateProfileSectionNamesA As Integer = INIFile.GetPrivateProfileSectionNamesA(array, array.GetUpperBound(0), Me._FileName)
            Dim arrayList As ArrayList = New ArrayList()
            If privateProfileSectionNamesA > 0 Then
                Dim index As Integer = 0
                For i As Integer = 0 To privateProfileSectionNamesA - 1
                    If array(i) = 0 Then
                        Dim text As String = Encoding.[Default].GetString(array, index, i).Trim()
                        index = i + 1
                        If text <> "" Then
                            arrayList.Add(text)
                        End If
                    End If
                Next
            End If
            Return arrayList
        End Function
        Public Function SectionExists(ByVal Section As String) As Boolean
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileSectionA(Section, stringBuilder, stringBuilder.Capacity, Me._FileName)
            Return Not (stringBuilder.ToString().Trim() = "")
        End Function
        Public Function ValueExits(ByVal Section As String, ByVal Key As String) As Boolean
            Return Not (Me.ReadString(Section, Key).Trim() = "")
        End Function
        Public Sub DeleteKey(ByVal Section As String, ByVal Key As String)
            Me.Write(Section, Key, Nothing)
        End Sub
        Public Sub DeleteSection(ByVal Section As String)
            INIFile.WritePrivateProfileSectionA(Section, Nothing, Me._FileName)
        End Sub
        Public Sub AddSection(ByVal Section As String)
            INIFile.WritePrivateProfileSectionA(Section, "", Me._FileName)
        End Sub
        Public Sub DeleteFile()
            If Me.FileExists() Then
                File.Delete(Me._FileName)
            End If
        End Sub
        Public Sub CreateFile()
            File.Create(Me._FileName).Close()
        End Sub
        Public Function FileExists() As Boolean
            Return File.Exists(Me._FileName)
        End Function
    End Class
End Namespace
      暂时没有评论