1

I want to start a cmd file and redirect the output to the console, but it doesn't work. What I have to do? I already set startInfo.RedirectStandardOutput = true.

com = "Parameter";

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = 
    new System.Diagnostics.ProcessStartInfo();

startInfo.FileName = Properties.Settings.Default.pathTo+ @"\Make\copy.cmd";
startInfo.Arguments = com;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
output = process.StandardOutput;
string ln;

while ((ln = output.ReadLine()) != null)
{
    Console.WriteLine(line);
}
process.WaitForExit();
Kjartan
  • 18,591
  • 15
  • 71
  • 96
tux007
  • 282
  • 1
  • 3
  • 17
  • possible duplicate of http://stackoverflow.com/questions/3829749/how-to-redirect-process-output-to-system-string?rq=1 and http://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net?rq=1 – Matten Apr 09 '13 at 11:00

2 Answers2

1

From MSDN

Set startInfo.UseShellExecute = false;

Then you can get the output as

Console.WriteLine(process.StandardOutput.ReadToEnd());
Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
0

Now, I have this solution:

                com = "Parameter";;

                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.FileName = Properties.Settings.Default.pathTo + @"\Make\copy.cmd";
                startInfo.Arguments = com;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                Console.WriteLine(process.StandardOutput.ReadToEnd());
                Console.WriteLine(process.StandardError.ReadToEnd());
tux007
  • 282
  • 1
  • 3
  • 17