S
Steve B.
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(outputStream); // An output stream will contains
the log
p = new Process();
p.StartInfo.FileName = "c:\\myapp.exe";
p.StartInfo.Arguments = string.Format(
"myarg=\"{0}\"",
arg
);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.WaitForExit();
}
catch (Exception exc)
{
sw.WriteLine(exc.ToString());
throw exc;
}
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
sw.Write(e.Data);
}
private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
sw.Write(e.Data);
}
}
Is it the right way ?
Thanks,
Steve
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(outputStream); // An output stream will contains
the log
p = new Process();
p.StartInfo.FileName = "c:\\myapp.exe";
p.StartInfo.Arguments = string.Format(
"myarg=\"{0}\"",
arg
);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.WaitForExit();
}
catch (Exception exc)
{
sw.WriteLine(exc.ToString());
throw exc;
}
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
sw.Write(e.Data);
}
private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
sw.Write(e.Data);
}
}
Is it the right way ?
Thanks,
Steve