Array Class Collections File String Console Data Directive Enum Exception If Interface Lambda Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows
An external process can be launched by a VB.NET application. This starts a program for the user. It runs a command-line program. We see examples of the Process.Start Function. This function is ideal for launching programs.
Note: Process.Start, found in System.Diagnostics, launches external programs, such as Word, Excel, a web browser—EXE programs.
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. In this program, the Main entry point is declared.
And: The Main Sub calls the Process.Start Shared method. It passes one parameter to Process.Start, the directory root.
Program that uses Process.Start: VB.NET Module Module1 Sub Main() Process.Start("C:\") End Sub End Module
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.
The Main entry point 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.
Tip: Instead of just passing "example.txt", you could pass "C:\Users\Sam\example.txt" to specify the absolute directory.
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
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. We 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
We 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.
Many organizations have certain web browsers specified.
These should be set in Windows.
You should avoid specifying IEXPLORE.EXE.
Many applications need to launch Word documents for editing or viewing. It is best to start Word in an external process. The example below is similar to the previous one, but it uses a custom FileName field and Arguments.
OpenMicrosoftWord sub. This part of the code assigns WINWORD.EXE as the FileName. Then it sets the path of the DOCX file as the Arguments property. The result is that Microsoft Word opens the file.String
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
Next, we use 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.
In this example, 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.
Arguments: The arguments String here is composed of five Strings concatenated together with the & operator.
Tip: It is useful to break in the Visual Studio debugger to verify your Arguments string.
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
The ProcessStartInfo class stores information about the process you want to run. This class encapsulates several fields needed for telling .NET how to start the FileName program. We can specify the FileName, Arguments and WindowStyle.
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: This property stores the arguments, including any -flags or filenames. It is a String value.
CreateNoWindow: Specifies that you want to run a command line program silently without flashing a console window.
WindowStyle: Use this to set windows as hidden, with the ProcessWindowStyle.Hidden enumeration value.
UserName, WorkingDirectory, Domain:
These properties control OS-specific parameters.
They are used in more complex situations.
The VB.NET language further provides a built-in Shell sub. Internally this subroutine simply calls Process.Start. It is useful for compatibility reasons. If you have used it often, it may be more convenient.Shell
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. We cover some issues related to Kill.Kill
A system has many running processes. With the Process.GetProcesses Function, and similar ones such as GetProcessesByName, we can get an array of all the processes. This can help with a variety of program requirements.Process.GetProcesses
What should you do when you have a long-running task that you don't want to block the main thread? Instead of launching another process, using a Thread is also possible. This is often more efficient.ThreadPool Sleep SyncLock
We saw examples and practical uses of Process.Start in VB.NET. I have used Process.Start in most programs I have written. It helps offload difficult processing to other applications. It is a critical part of a well-designed application.