How can I run another application or batch file from my Visual Basic .NET code?
Posted by: Duncan Mackenzie, MSDN
This post applies to Visual Basic .NET 2002/2003
Suppose you want to run a command line application, open up another Windows program, or even bring up the default web browser or email program... how can you do this from your VB code?
The answer for all of these examples is the same, you can use the classes and methods in System.Diagnostics.Process to accomplish these tasks and more.
Example 1. Running a command line application, without concern for the results:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("C:\listfiles.bat")
End Sub
Example 2. Retrieving the results and waiting until the process stops (running the process synchronously):
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim psi As New _
System.Diagnostics.ProcessStartInfo("C:\listfiles.bat")
psi.RedirectStandardOutput = True
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
Dim listFiles As System.Diagnostics.Process
listFiles = System.Diagnostics.Process.Start(psi)
Dim myOutput As System.IO.StreamReader _
= listFiles.StandardOutput
listFiles.WaitForExit(2000)
If listFiles.HasExited Then
Dim output As String = myOutput.ReadToEnd
Debug.WriteLine(output)
End If
End Sub
Example 3. Displaying a URL using the default browser on the user's machine:
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim myTargetURL As String = "https://www.duncanmackenzie.net"
System.Diagnostics.Process.Start(myTargetURL)
End Sub
By the way, you are much better off following the third example for URLs, as opposed to executing IE with the URL as an argument. The code shown here will launch the user's default browser, which may or may not be IE; you are more likely to provide the user with the experience they want and you will be taking advantage of the browser that is most likely to have up-to-date connection information.
Comments
Anonymous
May 31, 2004
Thanks,
I have soem code smith templates that create a bat file that I can run to compile the code. Now I can take the next step and compile.
BertAnonymous
June 23, 2004
The comment has been removedAnonymous
July 13, 2004
How do I code this?
"C:My InstallationsMy ProjectMediaDefaultDisk ImagesDisk1Data1.cab" -i"C:My FolderISCab.ini" -rAnonymous
July 23, 2004
john-
shot in the dark, but what about:
===
dim process as string = """C:My InstallationsMy ProjectMediaDefaultDisk ImagesDisk1Data1.cab"" -i""C:My FolderISCab.ini"" -r"
System.Diagnostics.Process.Start(Process)
===
I just ran that off, so it's untested, maybe it will work?
marko r.Anonymous
September 28, 2004
How can I run another application or batch file from my Visual Basic .NET code?Anonymous
January 15, 2008
PingBack from http://irfanmunir.wordpress.com/2007/10/25/csharp-faqs/Anonymous
July 11, 2008
PingBack from http://cooper.onlinevidssite.info/executevbprgramfrombatchfile.htmlAnonymous
July 11, 2008
PingBack from http://shayna.onlinepokervidsdirect.info/executevisualbasicprogramfromabatchfile.htmlAnonymous
January 21, 2009
PingBack from http://www.keyongtech.com/2396557-calling-a-bat-program-in