2

Summary:

I have a WinForm application doing some works and writing some output with System.Console, Here is the code:

static int Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Console.WriteLine("Some String");

    return 1;
}

Suppose we name it SubApp.exe

In another program, I want to execute this subApp.exe and read the output which it creates with System.Diagnostics.Process. Here is my code:

System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

p.Start();
p.WaitForExit();

string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
int exitCode = p.ExitCode;

Question:

But unfortunately, both output and error is empty and even worse the exitCode is 0. What is the problem? Am I doing the procedure in the right way?

8
  • Please paste your actual code and only omit what you feel is private info. It's hard to go off of massaged and untested code. For example you have System.Diagnosis. And just off the top of my head, double check you are actually pointing at the correct .exe (perhaps accidentally pointed at Debug instead of Release or vice-versa). Commented May 10, 2016 at 17:11
  • you code is wrone here System.Diagnostics.Process p = System.Diagnosis.Process.Start("SubApp.exe", "Some Parameter"); you should use System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter"); you have used Diagnosis Commented May 10, 2016 at 17:15
  • and after fix your code is working fine Commented May 10, 2016 at 17:16
  • @TyCobb and @mostafizur, It's my mistake, I mean Diagnostics Commented May 10, 2016 at 18:09
  • ok isn't my present solution not working ? Commented May 10, 2016 at 18:10

1 Answer 1

4

Make your SubApp to write argument to the console then it will return the console output to the process output

static int Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Console.WriteLine(args[0]); // this will print only 'Some' part of argument all string will be in args seperating by space
    Console.WriteLine(args[1]); // this will print "Parameter"

    return 1;
}

then call your SubApp

System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

p.Start();

string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();

p.WaitForExit();

int exitCode = p.ExitCode;

after executing this you will get Some Parameter into your output variable

Sign up to request clarification or add additional context in comments.

9 Comments

I don't have any problem with Arguments, My problem is output of the process, It is empty always.
my present solution is ok I have tested it just now, make sure your other things are ok
Do you create a winform application or just console one?
I have create a console application as you have
Did you receive 1 as exitCode?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.