0

When working with a command line program, via a c# class method.

How do you determine if the commandline program was successfully executed and the operation it has performed is ok or has failed?

Also how do you get the screen commandline output into the c# class method?

001
  • 62,807
  • 94
  • 230
  • 350
  • possible duplicate of [Capturing the Console Output in .NET (C#)](http://stackoverflow.com/questions/186822/capturing-the-console-output-in-net-c) – agent-j Jul 04 '11 at 16:46

5 Answers5

3

You can use the Process class to execute a command line command.

The following code captures the standard output to output, and assigns the processes exit code to exitCode.

using (Process p = new Process())
{
    p.StartInfo.FileName = exeName;
    p.StartInfo.Arguments = args;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    int exitCode = p.ExitCode;
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I will also write the console app, how do you set the exitCode in the console app? just return the code after it completes execution right? – 001 Jul 04 '11 at 16:59
  • Just return the errorlevel code from Main(). See http://msdn.microsoft.com/en-us/library/0fwzzxz2.aspx. – Jonathan Wood Jul 04 '11 at 17:01
1

Something like:

Process mycommand = new Process();
mycommand.StartInfo.FileName = "myexe.exe";
mycommand.StartInfo.Arguments = "param1";
mycommand.StartInfo.UseShellExecute = false;
mycommand.StartInfo.RedirectStandardOutput = true;
mycommand.Start();    
Console.WriteLine(mycommand.StandardOutput.ReadToEnd());
mycommand.WaitForExit();

You usually determine an exe's state wether the exit code is 0, but that is arguably down to the writer of the exe

BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

I assume you're using the Process class to call the command line app.

You can find the exit code of the process using Process.ExitCode. You can redirect its standard output by setting ProcessStartInfo.RedirectStandardOutput before starting it, and then either using Process.StandardOutput or the Process.OutputDataReceived event.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Take a look at this questionenter link description here.

The additional information you might need is process.ExitCode to see if it was sucessful. Of course, the Main method of the console app must return an exit code when it is unsuccessful, which many do not.

Community
  • 1
  • 1
agent-j
  • 27,335
  • 5
  • 52
  • 79
0

For this, you use the Process.Start method. You can control how the process runs with the passed in ProcessStartInfo:

var myProcess = Process.Start(new ProcessStartInfo {
  FileName = "process.exe",
  UseShellExecute = false,
  RedirectStandardOutput = true,
  CreateNoWindow = true
});
if (!myProcess.WaitForExit(5000)) { // give it 5 seconds to exit
  myProcess.Kill();
}
if (myProcess.ExitCode != 0) {
  // error!
}
var output = myProcess.StandardOutput.ReadToEnd(); // access output
Jordão
  • 55,340
  • 13
  • 112
  • 144