1

I am trying to open the console application from my windows forms application. I have used the below code. The code is opening the Console application but not displaying anything (showing only black screen). But I am successfully able get the console output using StandardOutput.

Process proc = new Process();
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.FileName = @"C:\Program Filex86)\ConsoleTool.exe";

proc.Start();
StreamWriter sw = proc.StandardInput; 
sw.WriteLine("init");

txtOutput.Text += proc.StandardOutput.ReadToEnd().Replace("\n", "\r\n");

txtOutput.Text += proc.StandardError.ReadToEnd().Replace("\n", "\r\n");
proc.WaitForExit();

How to get the display on console window that is opened using code?

4
  • Please check your path. It has a ) in it. That maybe the reason you get a Black Screen!!!!!1 Commented Apr 19, 2016 at 6:39
  • 2
    for one you are redirecting the StandardOutput so it will never display any information in the Console's window. You will need to manually do it yourself, see stackoverflow.com/q/8787123/479512 and stackoverflow.com/questions/2547428/… Commented Apr 19, 2016 at 6:43
  • @ Mark is there any way to display on console and get that data in my win form application ? Commented Apr 19, 2016 at 6:53
  • You have disconnected your console from all input and output and there is no way that I know to get the information back into it, you will need to do like Han's suggests in his accepted answer in the second link. Commented Apr 19, 2016 at 6:59

1 Answer 1

1

Use AllocConsole function to open a console window and then use Console.WriteLine or other Console functions normally!

Example:

[System.Runtime.InteropServices.DllImport("kernel32")]  
extern static void AllocConsole();  
//....  
AllocConsole();  
Console.WriteLine("hello world");  
Console.ReadKey();
Sign up to request clarification or add additional context in comments.

Comments

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.