Hi


I need a vb.net example code to execute a batch file when a button is clicked in my application .. The .bat file is in a different location and it must be called and executed.... Thanks in advance..

Dani AI

Generated

Nice and simple examples from and . If you need something a bit more robust (paths with spaces, remote paths, output capture, and a timeout), run the batch through cmd.exe and set the working directory and redirects explicitly.

Imports System.Diagnostics
Imports System.IO

Private Sub RunBatch(scriptPath As String)
    Dim psi As New ProcessStartInfo() With {
        .FileName = "cmd.exe",
        .Arguments = "/c """ & scriptPath & """",  ' quote the .bat path
        .WorkingDirectory = Path.GetDirectoryName(scriptPath),
        .UseShellExecute = False,
        .RedirectStandardOutput = True,
        .RedirectStandardError = True,
        .CreateNoWindow = True
    }

    Using p As New Process()
        p.StartInfo = psi
        p.Start()

        Dim stdout As String = p.StandardOutput.ReadToEnd()
        Dim stderr As String = p.StandardError.ReadToEnd()

        If Not p.WaitForExit(60000) Then  ' 60s safety net
            Try : p.Kill() : Catch : End Try
            Throw New TimeoutException("Batch did not finish in time.")
        End If

        Dim exitCode As Integer = p.ExitCode
        ' TODO: log stdout/stderr and check exitCode as needed
    End Using
End Sub

Practical tips that trip people up:

  • Quote everything with spaces. Also quote each argument you pass to the .bat.
  • Set WorkingDirectory if the .bat uses relative paths.
  • For files on another machine, prefer a UNC path like \server\share\script.bat. Mapped drive letters often do not exist for services or IIS.
  • Do not run this on the UI thread in WinForms/WPF. If targeting .NET 4.5+, call it via Task.Run to keep the UI responsive.
  • ASP.NET note: Process.Start runs on the server, under the app pool identity. Make sure that identity has NTFS and share permissions to the .bat and its resources. For long-running or high-latency work, consider queuing to a Windows service or a scheduled task instead of blocking the web request.

Recommended Answers

All 4 Replies

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Dim p As New System.Diagnostics.Process()
		p.StartInfo.FileName = "C:\\test.bat"
		p.Start()
	End Sub

You can also call p.WaitForExit() if you want your application to hold up until the bat file is done running.

Try out this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim p As New Process()
p.StartInfo.FileName = "E:\Users\Tom\Desktop\test.bat"
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal
p.Start()
p.WaitForExit()
p.Close()
MessageBox.Show("App closed now")
End Sub

thanks a lot my problem was solved..

thanks a lot my problem was solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.