.bat Datei im Hintergrund ausführen

  • VB.NET

Es gibt 40 Antworten in diesem Thema. Der letzte Beitrag () ist von Dodo.

    Wenn dies dein verwendeter Code ist, auch kein Wunder, den dort wird die genannte Funktion nicht angewendet:

    VB.NET-Quellcode

    1. Dim p As New Process()
    2. p.StartInfo.FileName = "C:\Identa\start.bat"
    3. p.StartInfo.WindowStyle = ProcessWindowStyle.Normal
    4. p.Start()
    5. System.Threading.Thread.Sleep(900)
    6. p.StartInfo.FileName = "C:\Identa\ende.bat"
    7. p.StartInfo.WindowStyle = ProcessWindowStyle.Normal
    8. p.Start()


    Bei dir muss es wie folgt aussehen

    VB.NET-Quellcode

    1. Class Form1
    2. Public Shared Sub RunHiddenExecutable(ByVal strFileName As String, ByVal strArguments As String, ByVal blnWaitForExit As Boolean, ByVal strLogPath As String)
    3. Dim blnOutputLogFile As Boolean = True
    4. If strLogPath Is Nothing OrElse strLogPath.Length = 0 Then
    5. blnOutputLogFile = False
    6. End If
    7. Dim sb As New System.Text.StringBuilder()
    8. Try
    9. Dim psi As New ProcessStartInfo()
    10. psi.WindowStyle = ProcessWindowStyle.Hidden
    11. psi.CreateNoWindow = True
    12. psi.FileName = strFileName
    13. psi.Arguments = strArguments
    14. psi.UseShellExecute = False
    15. psi.RedirectStandardOutput = True
    16. Dim p As Process = System.Diagnostics.Process.Start(psi)
    17. sb.Append(p.StandardOutput.ReadToEnd())
    18. If blnWaitForExit Then
    19. p.WaitForExit()
    20. End If
    21. Catch ex As Exception
    22. sb.Append("Exception Occured:" & Constants.vbCrLf)
    23. sb.Append(ex.ToString())
    24. Throw
    25. End Try
    26. End Sub
    27. Sub Form_Load(ByVal .. hier das übliche) As Me.Handles
    28. RunHiddenExecutable("C:\Pfad\zur\batch\start.bat", "", false, Nothing)
    29. System.Threading.Thread.Sleep(900)
    30. RunHiddenExecutable("C:\Pfad\zur\batch\end.bat", "", false, Nothing)
    31. End Sub
    32. End Class


    PS: Wenn die Text beim Beenden des Progs geschrieben wird, Starte das Prog aus Basic heraus, versteckt, und führe nur den taskkill Befehl über die Batch aus.