C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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
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("https://www.duckduckgo.com/?q=" & term)
End Sub
End Module
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
Arguments: This property stores the arguments, including any -flags or filenames. We concatenate strings together for the full string.
7-Zip Command-LineFileName: This is the program or file name we want to run. We can set it to a file such as "example.txt" or an executable name.
CreateNoWindow: Specifies that we 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.
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
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.
For Each, ForUses: With GetProcesses, we can scan the process list to see if any instances of a certain application (like Excel) are running.
Also: Process.GetProcesses() is effective in programs that analyze memory usage on a computer, in a diagnostic tool.
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
...
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
However: The Process type is probably better overall. More developers, such as those working in C#, may be accustomed to it.