0

Is it possible to control/write to a console from another. I have tested the Console.WriteLine, but nothing happend. I appreciate any help.


I meant write to a console from another class. Sorry for the misleading. I have a server class (Server.cs) and the main class (Program.cs). The server class is going to write some info about connection, and stuff like that, to the console.

7
  • I believe if it can be done(which I believe it can't), it's with win32API. Commented Apr 26, 2012 at 21:29
  • are you looking for console redirection msdn.microsoft.com/en-us/library/system.console.setout.aspx Commented Apr 26, 2012 at 21:30
  • @corn3lius. Console isn't a TextReader. And it's static so how would you pass it's reference? ... Commented Apr 26, 2012 at 21:31
  • Do you have control of the source code of the "other" console window? If so, you may be able to DuplicateHandle the console handle (GetStdHandle) and send it to the controlling program using some form of interprocess communication. I haven't tried it though. Commented Apr 26, 2012 at 21:34
  • 1
    Sounds to me like you need to clarify the question a bit. What are you trying to do? Commented Apr 26, 2012 at 21:46

2 Answers 2

1

To write to the calling console, your application needs to be marked as a Console application in project settings. If you write to the console in a UI application, your process will create a new one and then write in it.

If you want to write to another existing console, I guess it could be possible using P-Invoke on the Win32Api functions such as AttachConsole, WriteConsole and FreeConsole.

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

Comments

0

If i'm not mistake, you want something like this

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = "ModelExporter.exe";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();

StreamReader myStreamReader = p.StandardOutput;

// Reads a single line of the programs output.
string myString = myStreamReader.ReadLine(); 

// This would print the string to the DOS Console for the sake of an example.
// You could easily set a textbox control to this string instead...
Console.WriteLine(myString);

p.Close();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.