C# 控制台输出

本文关键字:输出 控制台 | 更新日期: 2023-09-27 18:32:19

是否可以从另一个控制台控制/写入控制台。我已经测试了Console.WriteLine,但没有任何反应。我感谢任何帮助。


我的意思是从另一个写入控制台。很抱歉误导。我有一个服务器类(服务器.cs)和主类(程序.cs)。服务器类将把一些关于连接的信息以及类似的东西写入控制台。

C# 控制台输出

若要写入调用控制台,需要在项目设置中将应用程序标记为Console应用程序。如果在 UI 应用程序中写入控制台,则进程将创建一个新控制台,然后在其中写入。

如果您想写入另一个现有的控制台,我想可以在Win32Api功能上使用P-Invoke,例如AttachConsoleWriteConsoleFreeConsole

如果我

没记错的话,你想要这样的东西

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