VB.NET Process.Start Examples

Arrow indicates movement

You have to launch an external process from your VB.NET application. This could be to start a program for the user or to run a command-line program. Here we see several examples and tips on the Process.Start Function, which is ideal for this solution.

These VB examples use the Process.Start Function. They open Microsoft Word, web browsers and text files.

Process.Start example

First, here is an example VB.NET program that uses Process.Start to open the file manager on your C:\ drive. When you run this example the root directory folder will open.

Program that uses Process.Start [VB.NET]

Module Module1
    Sub Main()
	Process.Start("C:\")
    End Sub
End Module

Description. The Main entry point is declared above and it contains a single line of code, which calls the Process.Start Shared method. It passes one parameter to Process.Start, the directory root.

Open TXT file

Here we see that when you specify a certain file for Process.Start to open, the default Windows file viewer for the file type will open. This is useful for text files, Microsoft Office files, and many other files.

Program that opens text file [VB.NET]

Module Module1
    Sub Main()
	' Open the file 'example.txt' at the current program's directory.
	' It will appear in the default text file viewer.
	Process.Start("example.txt")
    End Sub
End Module

Description. The Main entry point subroutine is declared above. It contains a comment and one call to Process.Start. The "example.txt" file must be found in the program's current directory. Notepad or any text viewer will open. This can be useful for "Read Me" files.

File paths. Instead of just passing "example.txt", you could pass "C:\Users\Sam\example.txt" to specify the absolute directory.

Search Google

In many programs, resources can be given to the user in the form of URLs. You can tell Windows to launch a web browser window with a specific URL. All you have to do is send Process.Start the URL. It is important that you do not specify IEXPLORE unless IE is required.

Program that launches web browser [VB.NET]

Module Module1
    Sub Main()
	SearchGoogle("VB.NET awesome")
    End Sub

    ''' <summary>
    ''' Open the user's default browser and search for the parameter.
    ''' </summary>
    Private Sub SearchGoogle(ByVal t As String)
	Process.Start("http://google.com/search?q=" & t)
    End Sub
End Module

Description. You can see that the subroutine SearchGoogle() is called with a String parameter. The SearchGoogle function is then run, and it calls Process.Start. The base URL for a Google search is specified. The query string is then set to the parameter.

Process.Start web browser search

Note on specifying browser executables. Many users and organizations have certain web browsers they use. However, these should be set at the system-level in Windows. Therefore, you shouldn't normally specify IEXPLORE.EXE here.

Open Microsoft Word

Many applications need to launch Word documents for editing or viewing. It is best to start Word in an external process for the greatest simplicity of code. The example below is similar to the previous one, but it uses a custom FileName field and Arguments.

Program that opens Microsoft Word [VB.NET]

Module Module1
    Sub Main()
	OpenMicrosoftWord("C:\Users\Sam\Documents\Office\Gears.docx")
    End Sub

    ''' <summary>
    ''' Open the path parameter with Microsoft Word.
    ''' </summary>
    Private Sub OpenMicrosoftWord(ByVal f As String)
	Dim startInfo As New ProcessStartInfo
	startInfo.FileName = "WINWORD.EXE"
	startInfo.Arguments = f
	Process.Start(startInfo)
    End Sub
End Module

Description. You can see that there are two methods, the first being the Main entry point. The OpenMicrosoftWord subroutine accepts one String ByVal.

OpenMicrosoftWord sub. This part of the code assigns WINWORD.EXE as the FileName, and then the path of the DOCX file as the Arguments. The result is that Microsoft Word opens the file.

Process.Start result

Run executable

Here we see how you can utilize a more complex Arguments String and also assign the WindowStyle for a command-line program. The command line program shown here is located at "C:\7za.exe", which is a command-line compression utility.

Program that launches executable [VB.NET]

Module Module1
    Sub Main()
	' One file parameter to the executable
	Dim sourceName As String = "ExampleText.txt"
	' The second file parameter to the executable
	Dim targetName As String = "Example.gz"

	' New ProcessStartInfo created
	Dim p As New ProcessStartInfo

	' Specify the location of the binary
	p.FileName = "C:\7za.exe"

	' Use these arguments for the process
	p.Arguments = "a -tgzip """ & targetName & """ """ & sourceName & """ -mx=9"

	' Use a hidden window
	p.WindowStyle = ProcessWindowStyle.Hidden

	' Start the process
	Process.Start(p)
    End Sub
End Module
Main method

Description. First, the Main subroutine entry point is declared and executed. Two variable Strings are declared. The two strings indicate two argument parameters we want to use with the executable.

ProcessStartInfo. ProcessStartInfo is another class that encapsulates several fields needed for telling .NET how to start the FileName program. We specify FileName, Arguments, and WindowStyle.

Arguments. The arguments String here is composed of five Strings concatenated together with the & operator. It is useful to break in the debugger to verify your Arguments string.

ProcessStartInfo This class stores information about the process you want to run.

FileName This is the program or filename you want to run. You can set it to a file such as "example.txt" or an executable name.

Arguments Stores the arguments, including any -flags or filenames.

CreateNoWindow Specifies that you want to run a command line program silently without flashing a console window. Useful for executing utility EXEs in the background.

WindowStyle Use this to set windows as hidden, with the ProcessWindowStyle.Hidden enumeration value.

UserName WorkingDirectory Domain These properties control OS-specific parameters, and are used in more complex situations where OS features are used extensively.

Kill processes

You can also kill a Process you have started using VB.NET code, or one that you accessed that was already running. There is a separate article that details the Kill subroutine.

Process Kill Example

Summary

The VB.NET programming language

Here we saw several examples and practical uses of Process.Start in VB.NET. I have used Process.Start in most programs I have written. It helps you offload difficult processing to other applications, and is a critical part of a well-designed application.

VB.NET Tutorials
.NET