nightWatcher nightWatcher - 1 year ago 67
C# Question

Using a command line program from within a C# application

I have written a C++ program (which executes from the command line), that works fine. Now I need to use it for my C# application. That is to say, I would like the output from my C++ program to be used in my C# application whenever it is called.

Is it possible? If so, how?

Any links or help would be appreciated.

Answer Source

You can use System.Diagnostics.Process to fire up your C++ program and redirect its output to a stream for use in your C# application. The information in this question details the specifics:

string command = "arg1 arg2 arg3"; // command line args
string exec = "filename.exe";      // executable name
string retMessage = String.Empty;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();

startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;

startInfo.UseShellExecute = false;
startInfo.Arguments = command;
startInfo.FileName = exec;

p.StartInfo = startInfo;

using (StreamReader output = p.StandardOutput)
    retMessage = output.ReadToEnd();


return retMessage;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download