Browse DevX
Sign up for e-mail newsletters from DevX


Managing Processes in .NET : Page 4

If you need to launch programs from within a .NET application, the Process class is the right tool. It not only allows you to start and stop a process, but it also provides detailed information about running processes.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Printing from the Shell
You can exploit the built-in support for shell verbs to enrich your applications with functions exposed by other programs. For example, suppose you need to print a text file. .NET has simplified nearly all operations compared to the Win32 platform. Printing is, perhaps, one of the very few exceptions. Even in .NET, writing the code that prints a document is a boring task. If you aren't too pretentious, you can content yourself with the printing engine in Notepad. The Notepad executable accepts the /p switch on command line and prints the specified document.

The shell API provides an interesting shortcut for printing text documents. The .NET Process API fully supports this shortcut through the Verb property. The code snippet below demonstrates how to print a text file using the print verb of the program registered to handle .txt files. By default, this program is notepad.exe.

Dim p As New Process Dim info As New ProcessStartInfo info.FileName = "test.txt" info.Verb = "print" p.StartInfo = info p.Start()

This snippet works regardless of which program you have registered on your computer to handle text files. In no way is it dependent on notepad.exe. It fails only if, for some reason, you don't have a program associated with text files. The Verbs collection returns the list of all custom verbs defined on the specified file. A feasible value for the Verb property is any string contained in the Verbs collection.

Note that when you invoke a verb, Windows launches the associated program and has it do some work on the specified file. This means that the text file will first be opened in notepad.exe (or whatever program is associated with .txt files) and then automatically printed using the user interface of the program. If you use verbs, you must ensure that you set UseShellExecute to True.

Capturing Process Output
A console program gets its input from the command line or the standard console input, and sends its output to the standard console output. In addition, it automatically sends error messages to a separate output stream.

Based on the Win32 API capabilities, you can replace the input, output, and error streams with custom streams. For example, suppose you use the following syntax from the console prompt:

program ?h >dump.txt

Any output the executable will generate is automatically redirected to the specified text file. The .NET process API supplies the same functionality through an object-oriented and easy-to-use set of properties. Let's see how to capture the output of a process to a string.

To begin, you must ensure that you've set the RedirectStandardOutput property to True. The default value is False. In the case of redirected output, the Process class accumulates the output into a memory stream instead of just sending it out to the console output. You can then access any byte output through the Process class' StandardOutput property. The following code snippet opens the console box, executes a dir statement, and captures the output. Figure 2 shows the result.

Figure 2: The output of a MS-DOS dir statement captured to a string and displayed through a MsgBox call.

Dim p As New Process Dim info As New ProcessStartInfo info.UseShellExecute = False info.CreateNoWindow = True info.FileName = "cmd.exe" info.Arguments = "/c dir *.*" info.RedirectStandardOutput = True p.StartInfo = info p.Start() p.WaitForExit() MsgBox (p.StandardOutput.ReadToEnd())

The file name is cmd.exe, which is the system executable for the console box. Notice that the file name and the arguments are separate entities to the Process class. Anything assigned to the FileName property is considered as the file name, including switches and parameters. The /c switch in the argument above indicates that the Command window must be closed immediately after the command terminates. Using the settings for UseShellExecute and CreateNoWindow guarantees that no black window will display, not even for a moment.

When the command has completed, the output stream, a memory stream object, contains all the bytes output by the program. You can read this stream using the classic API of a Stream object. For example, the ReadToEnd method reads all the contents of the stream to a string.

Is there any concrete advantage in running console applications in a hidden window? Well, that depends on what your console program does. If your console program provides an essential feature not otherwise exploitable then this trick sets an important milestone on the way to embedding that functionality into your applications. You know how to execute that program in an invisible window; you know how to configure it through its command line; you know how to capture its output. By adding a bit of parsing on the generated output you can easily figure out what the program did, its results, and return code. To demonstrate this possibility, I'll build a small class for zipping files.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date