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?

Antony
  • 1,221
  • 1
  • 11
  • 18
Niranjan NT
  • 165
  • 1
  • 19
  • Please check your path. It has a `)` in it. That maybe the reason you get a Black Screen!!!!!1 – Abhishek Kumar Apr 19 '16 at 06:39
  • 2
    for one you are redirecting the [StandardOutput](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.110).aspx) so it will never display any information in the Console's window. You will need to manually do it yourself, see http://stackoverflow.com/q/8787123/479512 and http://stackoverflow.com/questions/2547428/net-console-textwriter-that-understands-indent-unindent-indentlevel – Mark Hall Apr 19 '16 at 06:43
  • @ Mark is there any way to display on console and get that data in my win form application ? – Niranjan NT Apr 19 '16 at 06: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. – Mark Hall Apr 19 '16 at 06:59

1 Answers1

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();
Antony
  • 1,221
  • 1
  • 11
  • 18
mehrdad safa
  • 1,081
  • 9
  • 10