views:

35484

answers:

10

How do I execute a command-line program from C# and get back the STD OUT results. Specifically, I want to execute DIFF on two files that are programmatically selected and write the results to a text box. Yes, I could figure this out for myself, but surely someone else has done something like it and I'm lazy...

A: 

This may not be the best/easiest way, but may be an option:

When you execute from your code, add " > output.txt" and then read in the output.txt file.

Kon
+39  A: 
// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "YOURBATCHFILE.bat";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();
Ray Jezek
It is customary to add an attribution when you cut-n-paste code for somewhere else. This was taken from http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
Rasmus Faber
Is there a way to do this without a batch file? Thing is, I need to send some parameters to the command. I'm using the xsd.exe <Assembly> /type:<ClassName>, so I need to be able to set both the Assembly and the ClassName, and then run the command.
Carlo
You can add arguments to your call through the `{YourProcessObject}.StartInfo.Arguments` string.
patridge
A: 

You will need to use ProcessStartInfo with RedirectStandardOutput enabled - then you can read the output stream. You might find it easier to use ">" to redirect the output to a file (via the OS), and then simply read the file.

[edit: like what Ray did: +1]

Marc Gravell
That forces you to write a file somewhere that you need permission for, need to find a location and a name for and mustn't forget to delete when you're done with it. Easier to use `RedirectStandardOutput` actually.
peSHIr
+2  A: 

You can launch any command line program using the Process class, and set the StandardOutput property of the Process instance with a stream reader you create (either based on a string or a memory location). After the process completes, you can then do whatever diff you need to on that stream.

Nick
+5  A: 
 System.Diagnostics.ProcessStartInfo psi =
   new System.Diagnostics.ProcessStartInfo(@"program_to_call.exe");
 psi.RedirectStandardOutput = true;
 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 psi.UseShellExecute = false;
 System.Diagnostics.Process proc System.Diagnostics.Process.Start(psi);;
 System.IO.StreamReader myOutput = proc.StandardOutput;
 proc.WaitForExit(2000);
 if (proc.HasExited)
  {
  string output = myOutput.ReadToEnd();
 }
Jeff Mc
+9  A: 

Here's a quick sample:

//Create process
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = strCommand;

//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = strCommandParameters;

pProcess.StartInfo.UseShellExecute = false;

//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;   

//Optional
pProcess.StartInfo.WorkingDirectory = strWorkingDirectory;

//Start the process
pProcess.Start();

//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();

//Wait for process to finish
pProcess.WaitForExit();
Jeremy
A: 

There is a ProcessHelper Class in PublicDomain open source code which might interest you.

Shaik Phakeer
+5  A: 

There one other parameter I found useful, which I use to eliminate the process window

pProcess.StartInfo.CreateNoWindow = true;

this helps to hide the black console window from user completely, if that is what you desire.

Peter
+2  A: 

Peter the Process.StartInfo.CreateNoWindow = true; is used to tell a console application to not create a new console.

If you want to hide the console for the user use it instead:

Process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Piou
+1  A: 

thanks a lot my friends! good results

Gato