Friday, September 22, 2006

วิธีการสั่งรัน Process โดยไม่เริ่มหน้าต่างใหม่

Capturing the output from a console application in C#
Submitted by rlipscombe on Mon, 2005-02-07 21:35.C#
The .NET framework class library provides a handy Process class for running (and monitoring) other applications.

Here's how to grab hold of a Stream object for the input and output of a process.


Process process = new Process();
process.StartInfo.FileName = fileName;
process.StartInfo.Arguments = arguments;

// We have to turn off UseShellExecute to redirect the input and output.
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;

// If we're a Windows.Forms application, we'll want to turn
// off the new console window that appears.
process.StartInfo.CreateNoWindow = true;

// We have to turn on EnableRaisingEvents to enable the Exited event.
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(OnExited);

// We can't get at the StandardInput and StandardOutput properties until
// after the process is started.
process.Start();

// The Process class gives us StandardInput and StandardOutput
// properties, but they're a StreamWriter and StreamReader.
StreamWriter inputWriter = process.StandardInput;
Stream inputStream = inputWriter.BaseStream;

// If you want the underlying stream, you need the BaseStream
// property.
StreamReader outputReader = process.StandardOutput;
Stream outputStream = outputReader.BaseStream;

StreamReader errorReader = process.StandardError;
Stream errorStream = errorReader.BaseStream;

Ref :: Capturing the output from a console application in C#

No comments: