我有一个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