是否可以从另一个控制台控制/写入控制台。我测试过Console.WriteLine,但什么也没发生。我很感谢你的帮助。
我的意思是从另一个类写入控制台。抱歉让人误解了。我有一个服务器类(Server.cs)和主类(Program.cs)。服务器类将向控制台写入一些有关连接的信息,以及类似的内容。
发布于 2012-04-26 13:38:10
若要写入调用控制台,需要在项目设置中将应用程序标记为Console
应用程序。如果您在UI应用程序中写入控制台,您的进程将创建一个新的进程,然后在其中写入。
如果您想要写入另一个现有的控制台,我想在Win32Api函数(如AttachConsole
、WriteConsole
和FreeConsole
)上使用P-Invoke是可能的。
发布于 2012-04-26 13:38:52
如果我没搞错,你想要这样的东西
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();
https://stackoverflow.com/questions/10341673
复制