Hi,
I'm written a Web Service that encapsulate the call to a command line
application.
The command line application is run using Process.Start, and I'd like to get
both standard ouput and standard error to create a log.
After some searches, I finally wrote this part of code :
public class MyClass {
private StreamWriter sw; // A Streamwriter where we can write the log
private Process p;
public void DoSomething(
string arg,
Stream outputStream
)
{
try
{
this.sw = new StreamWriter(ou tputStream); // An output stream will contains
the log
p = new Process();
p.StartInfo.Fil eName = "c:\\myapp.exe" ;
p.StartInfo.Arg uments = string.Format(
"myarg=\"{0}\"" ,
arg
);
p.StartInfo.Use ShellExecute = false;
p.StartInfo.Red irectStandardOu tput = true;
p.StartInfo.Red irectStandardEr ror = true;
p.OutputDataRec eived += new DataReceivedEve ntHandler(p_Out putDataReceived );
p.ErrorDataRece ived += new DataReceivedEve ntHandler(p_Err orDataReceived) ;
p.Start();
p.WaitForExit() ;
}
catch (Exception exc)
{
sw.WriteLine(ex c.ToString());
throw exc;
}
}
void p_ErrorDataRece ived(object sender, DataReceivedEve ntArgs e)
{
sw.Write(e.Data );
}
private void p_OutputDataRec eived(object sender, DataReceivedEve ntArgs e)
{
sw.Write(e.Data );
}
}
Is it the right way ?
Thanks,
Steve