Menu

Executing a command line (CMD) utility (.exe) and reading the output it generate using C#

From time to time I have come across simple task – not really real development work, that require executing a exe file and getting some process done. In most cases, some old C/C++ else .exe (I know !!!, I am not the one using it) files that end-user use to get some work done. Ex: pass an image path of a vehicle to a .exe file and extract number plate, but one day get a backlog of 1000 images (OK, maybe it’s not the actual scenario – but let’s imagine it.)

So this is how you execute .exe file and read the output generated using C#

var proc = new Process
{
  StartInfo = new ProcessStartInfo
  {
    FileName = "path to my .exe",
    Arguments = "parameter-A parameter-B",  
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true
   }
};

proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
   var jsonString = proc.StandardOutput.ReadLine();
   Console.WriteLine(jsonString);
}

 

Leave a comment