C# 读取标准输出的问题
这些让我难住了。因此,我正在用 C# 编写一个程序,为 Terraria 服务器提供 GUI 前端。我尝试了多种方法,但总是遇到同样的问题。 首先,我使用 System.Diagnostic 启动该过程。 然后我尝试了几件事,例如使用 BeginOutputReadLine 异步读取控制台输出或创建后台工作程序来执行以下操作:
while (!terrariaProcess.StandardOutput.EndOfStream)
{
consoleOutput.Items.Add(terrariaProcess.StandardOutput.ReadLine());
}
但输出总是混乱的。 例子 它应该是:(如果我使用 cmd 执行它,它会是什么样子)
Terraria Server v1.0.6.1
Choose World:
1 Test
n New World
d <number> Delete World
>>n
Choose size:
1 Small
2 Medium
3 Large
>>3
Enter World Name:
>>Hello World
但是我的程序将其读取为:
Terraria Server v1.0.6.1
1 Test
n New World
d <number> Delete World
>>n
Choose World: Terraria Server v1.0.6.1
1 Small
2 Medium
3 Large
>>3
Choose size: Terraria Server v1.0.6.1
>>Hello World
无论我使用什么方法,它都会执行此操作。有人可以帮忙吗?我(又)是个白痴吗?
编辑: 根据乔恩的要求,我制作了一个小型控制台应用程序来尝试做同样的事情。我在检查循环内的控制台输入时遇到一些问题,因此我只能测试第一个提示,但它似乎仍然损坏。 我的代码:
Process terrariaProcess;
terrariaProcess = new Process();
terrariaProcess.StartInfo.FileName = "TerrariaServer.exe";
terrariaProcess.StartInfo.UseShellExecute = false;
terrariaProcess.StartInfo.RedirectStandardInput = true;
terrariaProcess.StartInfo.RedirectStandardOutput = true;
terrariaProcess.StartInfo.CreateNoWindow = true;
terrariaProcess.Start();
while (!terrariaProcess.StandardOutput.EndOfStream)
{
Console.WriteLine(terrariaProcess.StandardOutput.ReadLine());
}
结果输出:
Terraria Server v1.0.6.1
1 Test
n New World
d <number> Delete World
This ones got me stumped. So I'm making a program in C# to provide a GUI frontend for the Terraria Server. I have tried several methods but I always get the same problem.
First I start the process using System.Diagnostic.
Then I have tried several things, such as asynchronously reading the console output using BeginOutputReadLine or creating a Background worker to execute the following:
while (!terrariaProcess.StandardOutput.EndOfStream)
{
consoleOutput.Items.Add(terrariaProcess.StandardOutput.ReadLine());
}
But the output always comes out muddled.
Example
It should be: (How it looks if I use cmd to execute it)
Terraria Server v1.0.6.1
Choose World:
1 Test
n New World
d <number> Delete World
>>n
Choose size:
1 Small
2 Medium
3 Large
>>3
Enter World Name:
>>Hello World
However my program reads it as:
Terraria Server v1.0.6.1
1 Test
n New World
d <number> Delete World
>>n
Choose World: Terraria Server v1.0.6.1
1 Small
2 Medium
3 Large
>>3
Choose size: Terraria Server v1.0.6.1
>>Hello World
It does this no matter what method I use. Can someone help please? Am I being an idiot (again)?
EDIT:
On Jon's request I have made a small console application to try to do the same thing. I am having some trouble checking for console input from within a loop so I can only test up to the first prompt but it still seems broken.
My code:
Process terrariaProcess;
terrariaProcess = new Process();
terrariaProcess.StartInfo.FileName = "TerrariaServer.exe";
terrariaProcess.StartInfo.UseShellExecute = false;
terrariaProcess.StartInfo.RedirectStandardInput = true;
terrariaProcess.StartInfo.RedirectStandardOutput = true;
terrariaProcess.StartInfo.CreateNoWindow = true;
terrariaProcess.Start();
while (!terrariaProcess.StandardOutput.EndOfStream)
{
Console.WriteLine(terrariaProcess.StandardOutput.ReadLine());
}
The result output:
Terraria Server v1.0.6.1
1 Test
n New World
d <number> Delete World
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用函数
Console.Out.Flush();
Call the function
Console.Out.Flush();