简体   繁体   English

C#控制台输出

[英]C# Console Output

Is it possible to control/write to a console from another. 是否可以从另一个控制台控制/写入控制台。 I have tested the Console.WriteLine, but nothing happend. 我已经测试过Console.WriteLine,但是什么也没发生。 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). 我有一个服务器类(Server.cs)和主类(Program.cs)。 The server class is going to write some info about connection, and stuff like that, to the console. 服务器类将向控制台写入一些有关连接的信息,诸如此类。

To write to the calling console, your application needs to be marked as a Console application in project settings. 要写入调用控制台,您的应用程序需要在项目设置中标记为Console应用程序。 If you write to the console in a UI application, your process will create a new one and then write in it. 如果您在UI应用程序中写入控制台,您的过程将创建一个新的控制台,然后在其中写入。

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 . 如果要写入另一个现有控制台,我想可以在Win32Api函数(例如AttachConsoleWriteConsoleFreeConsole上使用P-Invoke。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM