У меня есть веб-приложение со страницей, полной пакетных файлов, которые пользователь может запускать, просматривать выходные данные и отправлять входные данные. Моя проблема возникает, когда процесс сталкивается с чем-то, что заставляет его приостанавливаться, например, с паузой или вопросом, который требует от пользователя нажатия Y или N для продолжения. Мы пойдем с паузой для целей этого вопроса.
Это мой командный файл:
pause
При запуске в Windows я получаю вывод, отображаемый на моем экране «Нажмите любую клавишу, чтобы продолжить ...», я нажимаю ввод, и он выходит. Но когда мое приложение запускает этот пакетный файл, я не получаю никакого вывода, но я знаю, чего он ждет, поэтому я нажимаю клавишу ввода, и только тогда я вижу вывод «Нажмите любую клавишу, чтобы продолжить…».
Я создал упрощенную версию своего кода в консольном приложении, и происходит то же самое, я получаю пустой экран, нажимаю клавишу ввода, а затем вижу "Нажмите любую клавишу, чтобы продолжить..."
Любая идея, как я могу получить эту строку вывода ДО того, как мне нужно будет нажать клавишу?
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);
}
}
}