2012-04-26 124 views
0

是否可以从另一个控制台/控制台写入控制台。我测试了Console.WriteLine,但没有发生。我感谢任何帮助。C#控制台输出


我的意思是写从另一个类的控制台。对不起,误导。 我有一个服务器类(Server.cs)和主类(Program.cs)。服务器类将写入关于连接的一些信息,以及类似的东西到控制台。

+0

我相信_if_这是可以做到(我相信它不能),它与Win32API的。 – gdoron 2012-04-26 21:29:56

+0

您是否在寻找控制台重定向 http://msdn.microsoft.com/en-us/library/system.console.setout.aspx – corn3lius 2012-04-26 21:30:26

+0

@ corn3lius。控制台不是“TextReader”。它是静态的,所以你如何传递它的参考? ** ... ** – gdoron 2012-04-26 21:31:25

回答

1

要写入调用控制台,需要在项目设置中将应用程序标记为Console应用程序。如果您在UI应用程序中写入控制台,您的过程将创建一个新过程并写入其中。

如果你想写入另一个现有的控制台,我想可能使用Win32Api功能上的P-Invoke功能,如AttachConsole,WriteConsoleFreeConsole

0

如果我没有搞错,你想是这样的

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();