This topic discusses how to start additional processes from a .NET application. This can be useful in scenarios where one application needs to selectively
|
|
Application #55: Use the File System
Application #73: Read From and Write to a Text File
|
|
The
System.Diagnostics
namespace contains the
Process
class, which contains a variety of
The sample application
Process.Start("notepad.exe")
The btnProcessStartInfo_Click event handler also launches Notepad, but it uses the ProcessStartInfo class to specify that it should be maximized.
Dim startInfo As New ProcessStartInfo("notepad.exe")
startInfo.WindowStyle = ProcessWindowStyle.Maximized
Process.Start(startInfo)
The btnUseVerb_Click event handler creates a text file and then creates a ProcessStartInfo object for the file.
Dim sw As New System.IO.StreamWriter("demofile_shell.txt")
sw.WriteLine("Eureka! You've printed!")
sw.Close()
Dim startInfo As New ProcessStartInfo("demofile_shell.txt")
Next, we
startInfo.Verb = "print"
This time, when we start the process we’ll maintain a reference to the returned Process instance so that we can call WaitForExit , which will force our application to block until the printing finishes.
Dim p As Process = Process.Start(startInfo)
p.WaitForExit()
Finally, we can delete the sample file and display a message box with the ExitCode of the process. In general, an exit code of 0 indicates a success and a nonzero number indicates some error condition.
System.IO.File.Delete("demofile_shell.txt")
MessageBox.Show("Printing finished with an exit code of " + _
p.ExitCode.ToString())
The btnCommandLine_Click event handler uses the Arguments property of the ProcessStartInfo object to send a command-line argument to a new instance of Windows Explorer.
Dim startInfo As New ProcessStartInfo("explorer.exe")
startInfo.Arguments = "/n"
Process.Start(startInfo)
The
Process
class provides a simple way to interact with other applications with very little overhead. This is especially

Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (O'Reilly))

Microsoft Visual Basic .NET Step by Step--Version 2003 (Step by Step (Microsoft))

Microsoft Visual Basic .NET Programmer's Cookbook (Pro-Developer)

Building Client/Server Applications Under VB .NET: An Example-Driven Approach