Module Update

    Dim F_PFAD = "F:\Programme\VERAGMonitoring\"
    Sub Main()

        If Not System.IO.Directory.Exists(F_PFAD) Then
            F_PFAD = getFByIP()
        End If


        If Not My.Computer.FileSystem.DirectoryExists(F_PFAD) Then
            MsgBox("ERROR_UPDATE_02: Update-Daten existieren nicht.", MsgBoxStyle.Critical, "ERROR")
        Else
            For i As Integer = 1 To 3 Step 1
                If Not DoesProcessExists("VERAGMonitoring") Then
                    delFiles()
                    FileCopier()
                    If Not System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory & "VERAGMonitoring.exe") Then
                        MsgBox("ERROR_UPDATE_03: VERAGMonitoring.exe konnte nicht gestartet werden.", MsgBoxStyle.Critical, "ERROR")
                        Environment.Exit(0)
                    End If
                    Process.Start("VERAGMonitoring.exe")
                    Environment.Exit(0)
                End If
                Threading.Thread.Sleep(1000)
            Next
            MsgBox("ERROR_UPDATE_01: Update konnte nicht durchgeführt werden. VERAGMonitoring.exe nicht beendet?", MsgBoxStyle.Critical, "ERROR")
        End If
        Environment.Exit(0)
    End Sub
    Private Sub FileCopier()
        For Each file As String In IO.Directory.GetFiles(F_PFAD) ' Ermittelt alle Dateien des Ordners 
            If cut_file(file).Contains("settings.txt") = False Then
                IO.File.Copy(file, AppDomain.CurrentDomain.BaseDirectory & cut_file(file), True) ' Kopiert die Dateien Next 
            End If
            For Each filed As String In IO.Directory.GetDirectories(F_PFAD) ' Ermittelt alle Unterordner des Ordners 
                My.Computer.FileSystem.CopyDirectory(filed, AppDomain.CurrentDomain.BaseDirectory & cut_file(filed), True)
            Next
        Next
    End Sub
    Private Function cut_file(ByVal file As String) As String ' Funktion zum Entfernen der Backslashs / Ordner While file.Contains("\") file = file.Remove(0, 1) End While Return file End Function 
        While file.Contains("\")
            file = file.Remove(0, 1)
        End While
        Return file
    End Function
    Private Sub delFiles()
        Try
            For Each file As String In IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory) ' Ermittelt alle Dateien des Ordners 
                If cut_file(file).Contains("VERAGMonitoringUPDATER") = False And cut_file(file).Contains("settings.txt") = False Then
                    My.Computer.FileSystem.DeleteFile(file) 'Löscht das ADMIN-Programm, außer den Updater
                End If
            Next
            For Each file As String In IO.Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory) ' Ermittelt alle Dateien des Ordners 
                If cut_file(file).Contains("VERAGMonitoringUPDATER") = False Then
                    System.IO.Directory.Delete(file, True)
                End If
            Next
        Catch ex As Exception
            MsgBox("ERROR_UPDATE_04: Fehler beim Löschen", MsgBoxStyle.Critical, "ERROR")
            Environment.Exit(0)
        End Try
    End Sub
    Private Function DoesProcessExists(ByVal PName As String) As Boolean
        If System.Diagnostics.Process.GetProcessesByName(PName).Length > 0 Then
            Return True
        End If
        Return False
    End Function


    Function getFByIP() As String
        Dim default_F = ""
        getFByIP = ""
        Try
            Dim IPADDR As System.Net.IPAddress
            IPADDR = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList(0)
            'MsgBox(IPADDR.ToString())

            For Each l In System.IO.File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory & "VERAGMonitoringUPDATER_Settings.txt")
                If l.Contains("|") Then
                    Dim sp = l.Split("|")
                    If IPADDR.ToString.Contains(sp(0).Replace("**", "")) Then
                        getFByIP = sp(1)
                    End If
                Else
                    default_F = l
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
        If getFByIP = "" Then
            If default_F <> "" Then
                getFByIP = default_F
            Else
                getFByIP = F_PFAD
            End If
        End If
    End Function

End Module