0

My env is Visual Studio 2008 C# and .Net. I like to call an executable from the code and pass parameters to it and then get the output. How do I do this?

Thanks Bruce

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Bruce
  • 2,133
  • 12
  • 34
  • 52
  • 1
    possible duplicate of [How To: Execute command line in C#, get STD OUT results](http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results) – Jeremy McGee Sep 26 '11 at 12:17

2 Answers2

3

Use Process.Start to start a process. How you give it data and get the output will depend on the executable, but you can pass command line arguments, and if it writes its output to a file, that will be simple. You can capture its standard output, standard error and standard input streams too - ideally use ProcessStartInfo to configure everything you need before starting the process.

Reading/writng data from the process via the standard output/error/input can be slightly tricky (as you may need to use multiple threads) so if you can specify command line arguments to specify input and output files, that would be ideal.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Further to @Jon's answer this link provides a number of useful tutorials: http://www.dotnetperls.com/process-start – Kane Sep 26 '11 at 12:19
2
processInfo.FileName =FileName.exe;
processInfo.WorkingDirectory = fileDirectory;
processInfo.RedirectStandardInput =false;
processInfo.CreateNoWindow =true;
processInfo.UseShellExecute =false;
Process startRestore = Process.Start(processInfo);
startRestore.WaitForExit();

Replace your file with "FileName.exe" and File path with fileDirectory. I hope it will be helpful to you.

Vero009
  • 612
  • 3
  • 18
  • 31