Recipe12.21.Launching and Interacting with Console Utilities


Recipe 12.21. Launching and Interacting with Console Utilities

Problem

You have an application that you need to automate and that takes input only from the standard input stream. You need to drive this application via the commands it will take over the standard input stream.

Solution

Say we needed to drive the cmd.exe application to display the current time with the TIME /T command (it is possible to just run this command from the command line, but this way we can demonstrate an alternative method to drive an application that responds to standard input). The way to do this is to launch a process that is looking for input on the standard input stream. This is accomplished via the Process class StartInfo property, which is an instance of a ProcessStartInfo class. The Process.Start method will launch a new process, but the StartInfo property controls many of the details of what sort of environment that process executes in.

First, make sure that the StartInfo.RedirectStandardInput property is set to true. This setting notifies the process that it should read from standard input. Then set the StartInfo.UseShellExecute property to false, because if you were to let the shell launch the process for you, it would prevent you from redirecting standard input.

Once this is done, launch the process and write to its standard input stream as shown in Example 12-11.

Example 12-11. RunProcessToReadStdIn method

 public static void RunProcessToReadStdIn( ) {     Process application = new Process( );     // Run the command shell.     application.StartInfo.FileName = @"cmd.exe";     // Turn on standard extensions.     application.StartInfo.Arguments = "/E:ON";     application.StartInfo.RedirectStandardInput = true;     application.StartInfo.UseShellExecute = false;     // Start it up.     application.Start( );     // Get stdin.     StreamWriter input = application.StandardInput;     // Run the command to display the time.     input.WriteLine("TIME /T");     // Stop the application we launched.     input.WriteLine("exit"); } 

Discussion

Once the input has been redirected, you can write into the standard input stream of the process by reading the Process.StandardInput property, which returns a StreamWriter. Once you have that, you can send things to the process via WriteLine calls, as shown earlier.

In order to use StandardInput, you have to specify true for the StartInfo property's RedirectStandardInput property. Otherwise, reading the StandardInput property throws an exception.

When UseShellExecute is false, you can use Process only to create executable processes. Normally the Process class can be used to perform operations on the file, like printing a Microsoft Word document. Another difference when UseShellExecute is set to false is that the working directory is not used to find the executable, so you should be mindful to pass a full path or have the executable on your PATH environment variable.

See Also

See the "Process Class," "ProcessStartInfo Class," "RedirectStandardInput Property," and "UseShellExecute Property" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net