Visual Basic Frequently Asked Questions

Maintained by Duncan Mackenzie and some great VB MVPs

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 = "http://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.

 

Published Sunday, May 30, 2004 1:43 AM by vbfaq
Filed under:

Comments

 

bert corderman said:

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.
Bert
May 31, 2004 11:06 AM
 

C# Frequently Asked Questions said:

June 2, 2004 2:56 AM
 

C# Frequently Asked Questions said:

June 2, 2004 2:58 AM
 

ShowUsYour said:

June 7, 2004 6:36 PM
 

Gabriele Rizzetto said:

This doesn't work if I want to run something with command line arguments - what then?

i.e.

System.Diagnostics.Process.Start("parp.exe /X") or something like that - seems not to like it.
June 23, 2004 11:00 AM
 

John Marshall said:

How do I code this?
"C:\My Installations\My Project\Media\Default\Disk Images\Disk1\Data1.cab" -i"C:\My Folder\ISCab.ini" -r
July 13, 2004 6:30 PM
 

marko said:

john-
shot in the dark, but what about:
===
dim process as string = """C:\My Installations\My Project\Media\Default\Disk Images\Disk1\Data1.cab"" -i""C:\My Folder\ISCab.ini"" -r"
System.Diagnostics.Process.Start(Process)
===
I just ran that off, so it's untested, maybe it will work?

marko r.
July 24, 2004 12:54 AM
 

John Qin said:

How can I run another application or batch file from my Visual Basic .NET code?
September 28, 2004 1:03 PM
 

FAQ C# (par Yannick Lejeune) said:

May 17, 2005 10:05 AM
 

FAQ C# (par Yannick Lejeune) said:

May 17, 2005 10:06 AM
 

FAQ C# (par Yannick Lejeune) said:

August 23, 2005 5:10 AM
 

MuNiRs said:

January 16, 2008 2:35 AM
 

execute vb prgram from batch file said:

July 11, 2008 12:33 PM
 

execute visual basic program from a batch file said:

July 11, 2008 2:51 PM
 

calling a .bat program in a vb .net program | keyongtech said:

January 22, 2009 12:34 AM
Anonymous comments are disabled

© 2010 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker