Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Redirecting the StandardInput, StandardError(stderr) and the StandarOutput Using the Process Class
By Edwin Lima

Short Description

This example has as its main goal to illustrate one of the many possibilities that we have while using the Process class.

Article

I can lunch any program from a C# application using the Process class. But what if I want to redirect the StandarInput(the keyboard), the StandardError(stderr) and the StandarOutput (the video)? Well, this can be achieved easily with the process class. First of all we need to instantiate a ProcessStartInfo class passing as constructor param the name of the app that we want to lunch and to set some parameters to be passed to the Process instances (p):

ProcessStartInfo psI = new ProcessStartInfo("cmd");

The property psI.UseShellExecute was set as false, to be able to redirect the StandardInput, etc. After the properties

 psI.RedirectStandardInput 
 psI.RedirectStandardOutput 
 psI.RedirectStandardError 
...are set to true.

To avoid the cmd annoying window we set as true the property psI.CreateNoWindow so no window will be created for the cmd app. Finally we set the p.StartInfo property to the instance of ProcessStartInfo that we have just created and voila , start the process.

To be able to capture the p.StandardInput, p.StandardOutput and p.StandardError we get the File Descriptors (StreamReaders and StreamWriter classes) to read the StandardOutput and StandardError and to write to the StandardInput. The read and write operations are performed as we do it in a normal file. (Hmm, does this way of working with the IO remind you the unix world?) The process cmd is closed when we close the p.StandardInput file descriptor.

At the very end we read the p.StandardOutput and p.StandardError file descriptors into the text box. See the complete code of the method that implements the whole stuff.

  private void start()
  {
   Process p = new Process();
   StreamWriter sw;
   StreamReader sr;
   StreamReader err;

   ProcessStartInfo psI = new ProcessStartInfo("cmd");
   psI.UseShellExecute = false;
   psI.RedirectStandardInput = true;
   psI.RedirectStandardOutput = true;
   psI.RedirectStandardError = true;
   psI.CreateNoWindow = true;
   p.StartInfo = psI;
   
   p.Start();
   sw = p.StandardInput;
   sr = p.StandardOutput;
   err = p.StandardError;

   sw.AutoFlush = true;
   if (tbComm.Text != "")
    sw.WriteLine(tbComm.Text);
   else
    //execute default command
    sw.WriteLine("dir \");
   
   sw.Close();

   textBox1.Text = sr.ReadToEnd();
   textBox1.Text += err.ReadToEnd();
  }
Download process.zip

-------------------------

Edwin Lima is a software engineer working for NEC Computers International BV in the Netherlands. Most of his time he's busy with data integration tasks and in his free time he plays around with C# and .NET related stuff (contact me at: edwinlima@hotmail.com)