這的人難住我了。所以我正在用C#編寫一個程序來爲Terraria服務器提供一個GUI前端。我嘗試了幾種方法,但我總是遇到同樣的問題。 首先我使用System.Diagnostic開始該過程。 後來我嘗試了好幾種東西,比如異步讀取使用BeginOutputReadLine控制檯輸出或創建一個後臺工作執行如下:C#與閱讀標準輸出問題
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
是否有可能,問題是你如何顯示輸出? –
我對此表示懷疑。我得到的每一行輸出都添加到ListBox中。我會嘗試將它們添加到一個字符串,雖然掛在秒 –
順便說一句,你花你的整個人生徘徊在你的鍵盤看着這個網站?因爲你總是回答我的問題。 (它總是我是一個白癡) –