我有一個Web應用程序,其中有一個用戶可以運行批處理文件的頁面,查看輸出併發送輸入。當進程遇到導致其暫停的事件(例如暫停)或需要用戶按Y或N以繼續的問題時,會出現我的問題。爲了這個問題我們會暫停。運行批處理文件並在暫停後讀取最後一行輸出
這是我的批處理文件:
pause
當在Windows中運行,我得到我的屏幕「按任意鍵繼續......」上顯示的輸出,我按enter鍵,並退出。但是當我的應用程序運行這個批處理文件時,我沒有得到任何輸出,但是我知道它在等待什麼,所以我按下回車鍵,然後纔看到輸出「按任意鍵繼續...」。
我在控制檯應用程序中創建了我的代碼的簡化版本,並且發生了同樣的情況,我得到一個空白屏幕,然後按Enter鍵,然後我看到「按任意鍵繼續...」
任何想法我怎麼去得到這一行的輸出之前我需要按下鍵?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace BatchCaller
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = @"C:\Projects\BatchCaller\BatchCaller\Test2.bat",
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
Process proc = new Process();
proc.StartInfo = psi;
proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
proc.Start();
proc.BeginOutputReadLine();
// Problem is not here, ignore this, just my temporary input method.
// Problem still occurs when these two lines are removed.
string inputText = Console.ReadLine();
proc.StandardInput.WriteLine(inputText);
proc.WaitForExit();
}
static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
// This method doesnt get called for output: "Press any key to continue..."
// Why?
if (e.Data != null)
Console.WriteLine(e.Data);
}
}
}
很好的問題。 +1。 – Kangkan
我想我已經用我的臨時輸入法混淆了人。忽略輸入,這不是問題,如果我從代碼中刪除它,我仍然看不到輸出「按任意鍵繼續...」。這是我想解決的問題。 – Owen
也從批處理文件中刪除@echo。它使問題更清楚,暫停顯示在屏幕上,但沒有「按任意鍵繼續...」 – Owen