2012-04-26 250 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();