3

i want to pass param to another exe file which is also developed by c#. i know how to pass parameter to exe file from my application. this way i can pass param to exe file

Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "param1 param2";
p.Start();
p.WaitForExit();

now demo.exe file will do some job and will return some data. i want to capture that data at my end. so guide what i alter in my code to capture response return by demo.exe file. help me with altered code. thanks

probably below solution may solve my issue. i will test it. When you create your Process object set StartInfo appropriately:

var proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "program.exe",
        Arguments = "command line arguments to your executable",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

then start the process and read from it:

proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    // do something with line
}
Mou
  • 15,673
  • 43
  • 156
  • 275
  • 1
    Possible duplicate of http://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output – Jan Köhler Apr 19 '15 at 11:30
  • Use `Process.ExitCode`: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode%28v=vs.110%29.aspx – Matjaž Apr 19 '15 at 11:31

4 Answers4

2

One possible solution is to use RedirectStandardOutput and store your result in a file:

using(Process proc = new Process())
{
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.FileName = <your exe>;
    proc.StartInfo.Arguments = <your parameters>;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.OutputDataReceived += LogOutputHandler;
    proc.Start();
    proc.BeginOutputReadLine();
    proc.WaitForExit();
}

private static void LogOutputHandler(object proc, DataReceivedEventArgs outLine)
{
    <write your result to a file here>
}
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • how to return value from the exe file which will be called from my application. suppose i am going to call demo.exe file. so what i need to write or how to return value from demo apps as a result my main apps can read that value. please guide me with code like how to return data. thanks – Mou Apr 20 '15 at 19:00
1

The easy solution is the old-fashioned process exit code.

You can use p.ExitCode to capture the result in your code once the process has terminated.

Also, demo.exe needs to set Environment.ExitCode before exiting.

Typically, 0 is used to report success.

Pascal
  • 116
  • 8
  • suppose demo.exe will return a string value which i want to capture from my application. just guide me what code i need to write? – Mou Apr 19 '15 at 11:38
  • Many different solutions can be used... Easy : you can output result to a file and read it in the other project. Less easy : you can do cross-process comunication with WCF as mentioned by @Beno – Pascal Apr 19 '15 at 11:40
1

You can check the ExitCode of the process if it just int. otherwise you can use WCF pipeline to communicate between the process

Beno
  • 721
  • 1
  • 10
  • 32
0

The return value for main is 'void' so you can't return any item. You can use standard output which is a stream to pass back info. An easy method is to return an XML stream back to the calling program which is easily parsed.

jdweng
  • 33,250
  • 2
  • 15
  • 20