VB.NET Process Examples (Process.Start)

Use the Process.Start Function. Open Microsoft Word, web browsers and text files.
Process. A process starts, performs a task and ends. With the Process type, from System.Diagnostics, we launch processes directly inside programs.
Functions. Many new tasks become possible, with little extra complexity. With Start we create a Process. With GetProcesses we check existing ones.
First example. Here is an example program. It uses Process.Start to open the file manager on your C:\ drive. When you run this example the root directory folder will open.

Tip: Process.Start, found in System.Diagnostics, launches external programs, such as Word, Excel, a web browser—EXE programs.

Main: The Main Sub calls the Process.Start Shared method. It passes one parameter to Process.Start, the directory root.

VB.NET program that uses Start Module Module1 Sub Main() Process.Start("C:\") End Sub End Module
Text file. 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 many files.

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

VB.NET program that opens text file Module Module1 Sub Main() ' Open the file at the current program's directory. ' It will appear in the default text file viewer. Process.Start("example.txt") End Sub End Module
Search. 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.

Here: The subroutine SearchDuckDuckGo() is called with a String parameter. The code is then run, and it calls Process.Start.

Note: You can specify any web address. The default browser on Windows is chosen automatically.

VB.NET program that launches web browser Module Module1 Sub Main() SearchDuckDuckGo("cat pictures") End Sub Private Sub SearchDuckDuckGo(ByVal term As String) ' Search DuckDuckGo for the specified term. ' ... Default browser is used. Process.Start("" & term) End Sub End Module
Microsoft Word. Many applications need to launch Word documents for editing or viewing. It is best to start Word in an external process. We use a custom FileName field and Arguments.

OpenMicrosoftWord: This code assigns WINWORD.EXE as the FileName. Then it sets the path of the DOCX file as the Arguments property.

Result: Microsoft Word opens the file. You will need to adjust the paths for it to correctly work.

VB.NET program that opens Microsoft Word 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
Executable. 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.7-Zip Command-Line

Here: 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.

VB.NET program that launches executable 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
ProcessStartInfo. This class stores information about the process you want to run. It has fields that tell .NET how to start the FileName program. We specify property values.

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.

Shell. 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

However: The Process type is probably better overall. More developers, such as those working in C#, may be accustomed to it.

GetProcesses. An operating system manages many processes. We can retrieve an Array of these processes with the Process.GetProcesses Function.

Example: We display the number of processes in the array by using the Length property.

Then: We use the For-Each loop to enumerate the Process objects. We display the ProcessName and the Id of each Process.

VB.NET program that uses Process.GetProcesses Module Module1 Sub Main() ' Get processes. Dim processes() As Process = Process.GetProcesses() Console.WriteLine("Count: {0}", processes.Length) ' Loop over processes. For Each p As Process In processes ' Display process properties. Console.WriteLine(p.ProcessName + "/" + p.Id.ToString()) Next End Sub End Module Output Count: 65 chrome/5116 LogonUI/3736 atiesrxx/832 svchost/1760 svchost/3136 svchost/768 firefox/2540 ...
GetProcesses, usage. The GetProcesses() Function has a variety of uses. In some programs, we start external programs such as Microsoft Excel.

Tip: With GetProcesses, we can scan the process list to see if any instances of Excel are running.

Tip 2: Process.GetProcesses() is effective in programs that analyze memory usage on a computer, in a diagnostic tool.

Kill. Let us examine the Kill Sub. You can also kill a Process you have started using VB.NET code, or one that you accessed that was already running.

Example: This simple program starts the "notepad" executable, which you are familiar with. It uses the Process.Start function for this.

Finally: It demonstrates the Kill method. At this point, the "notepad" process is no longer running.

VB.NET program that uses Kill Module Module1 Sub Main() ' Start the notepad.exe Process. Dim p As Process = Process.Start("notepad") Console.WriteLine("Started") ' Sleep for one second. Threading.Thread.Sleep(1000) ' Terminate. p.Kill() Console.WriteLine("Killed") End Sub End Module
Threads, Sleep. Instead of a process, we can a use a Thread. The Sleep method pauses a program. It just ceases execution for a specified amount of time.Sleep
Threads, SyncLock. The SyncLock construct is useful when dealing with threads. It prevents multiple threads from accessing a piece of code at once.SyncLock
ThreadPool. The ThreadPool type allows the efficient creation (and reuse) of threads. This approach is sometimes useful. It can enhance batch processing.ThreadPool
A summary. Processes are operating system level threads. They are started with the Process.Start function. By starting processes, we introduce many new capabilities to programs.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls