Imports System.Text

Public Class ArConfig
    Private Function NewFileAndWrite(ByVal directory As String, ByVal fill As String)
        Dim sb As New StringBuilder
        sb.Append(fill)
        If My.Computer.FileSystem.DirectoryExists(directory) = True Then
            Try
                My.Computer.FileSystem.DeleteFile(directory)
            Catch ex As Exception
                Return False
            End Try
        End If
        Try
            IO.File.WriteAllText((directory), sb.ToString, System.Text.Encoding.GetEncoding(950))
        Catch ex As Exception
            Return False
        End Try

        Return True
    End Function
    Private Function ReadFileContain(ByVal Filename As String, ByVal Contain As String)
        On Error Resume Next
        Dim code As String = Nothing
        If System.IO.File.Exists(Filename) = True Then

            Dim objReader As New System.IO.StreamReader(Filename, System.Text.Encoding.GetEncoding(950))

            Do While objReader.Peek() <> -1

                code = objReader.ReadLine()
                If code.Contains(Contain) Then
                    objReader.Dispose()
                    Return code
                End If
            Loop
            objReader.Dispose()
        Else

            Return Nothing
        End If
    End Function
    Private Function ReadFile(ByVal Filename As String)
        On Error Resume Next
        Dim code As String = Nothing
        If System.IO.File.Exists(Filename) = True Then

            Dim objReader As New System.IO.StreamReader(Filename, System.Text.Encoding.GetEncoding(950))
            Dim sb As New StringBuilder
            Do While objReader.Peek() <> -1
                code = objReader.ReadLine()
                sb.AppendLine(code)
            Loop
            objReader.Dispose()
            Return sb.ToString
        Else
            Return Nothing
        End If
    End Function

    Public Sub WriteConfig(Apploc As String, Item As String, Content As String)
        On Error Resume Next
        Item = Item.ToLower()
        Dim configloc As String = Apploc & "config.aro"
        Dim configcontent As String = ReadFile(Apploc & "config.aro")
        configcontent = configcontent.Replace(ReadFileContain(configloc, Item & "="), Item & "=" & Content)
        NewFileAndWrite(configloc, configcontent)

    End Sub

    Public Function ReadConfig(Apploc As String, Item As String)
        On Error Resume Next
        Item = Item.ToLower()
        Dim configloc As String = Apploc & "config.aro"
        Return ReadFileContain(configloc, Item & "=").replace(Item & "=", Nothing)
    End Function

    Public Function ReadAPI(aroname As String, Apploc As String, Item As String)
        On Error Resume Next
        If aroname.Contains(".aro") = True Then
            aroname = aroname.Replace(".aro", "")
        End If
        'MsgBox(Apploc & aroname & ".aro")
        Item = Item.ToLower()
        Dim configloc As String = Apploc & aroname & ".aro"
        Return ReadFileContain(configloc, Item & "=").replace(Item & "=", Nothing)
    End Function
    Public Sub WriteAPI(aroname As String, Apploc As String, Item As String, Content As String)
        On Error Resume Next
        If aroname.Contains(".aro") = True Then
            aroname = aroname.Replace(".aro", "")
        End If
        Item = Item.ToLower()
        Dim configloc As String = Apploc & aroname & ".aro"
        Dim configcontent As String = ReadFile(Apploc & aroname & ".aro")
        configcontent = configcontent.Replace(ReadFileContain(configloc, Item & "="), Item & "=" & Content)
        NewFileAndWrite(configloc, configcontent)

    End Sub
End Class
