Flylib.com

Books Software

 
 
 

Application 59: Use the Process Class and Shell Functionality


Application #59: Use the Process Class and Shell Functionality

This topic discusses how to start additional processes from a .NET application. This can be useful in scenarios where one application needs to selectively enlist the help of another application or service.

start sidebar
Building Upon…

Application #55: Use the File System

Application #73: Read From and Write to a Text File

end sidebar

New Concepts

The System.Diagnostics namespace contains the Process class, which contains a variety of methods for spawning processes. The easiest way to launch a process is to call the shared Start method and provide the file path to the executable or file of interest. You can specify any file type that has an executable mapped to its Open action. The Start method is overloaded, so you can optionally specify command-line arguments to pass to the new process. If you want even greater control over how the new process is started, you can create an instance of the ProcessStartInfo class and pass it into the Start method. The ProcessStartInfo class provides many properties that affect how an application is launched. The WindowStyle property determines how the new process will display: Maximized , Minimized , Normal , or Hidden . The Arguments property provides another way to supply command-line arguments. An especially powerful member is the Verb property. Setting this property allows you to perform an action on the file other than opening it. The only restriction is that the verb you supply must be defined for that file type on the system.

Code Walkthrough

The sample application demonstrates how to use the Process class to launch applications. The btnStartProcess_Click event handler simply launches Notepad using the shared Start method.

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 indicate that we want to print the document instead of opening it.

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)

Conclusion

The Process class provides a simple way to interact with other applications with very little overhead. This is especially convenient for interaction with applications whose only automation interface might be through the command line.