如何识别流程是否需要输入?

时间:2015-05-11 08:10:07

标签: c# process system.diagnostics redirectstandardoutput

我正在使用.NET Framework中的System.Diagnostics.Process类运行命令提示实用程序。在某些情况下,此命令提示实用程序可能需要一些用户输入,有时它还会完成任务而无需用户输入任何内容。 以下是我为完成此过程所做的工作:

    var process = new Process();
    process.StartInfo = new ProcessStartInfo([PATH_TO_EXE], arguments);
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.UseShellExecute = false;
    process.Start();

问题是我不知道如何确定这个过程是否需要输入?

1 个答案:

答案 0 :(得分:0)

我能找到的唯一有效的方法。无论响应时间长短。发送这样的命令后也添加回显。比只是检查回声就知道何时停止等待。

var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;

process.StartInfo.FileName = Settings.MongoPath;

// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardInput = true;

process.StartInfo.Arguments = ConnString;

process.Start();

// this is key, this returns 1 once previous command in argument is done executing
process.StandardInput.WriteLine("{ping:1}");

var current_line = string.Empty;
var OutputList = new List<string>();
while (!process.StandardOutput.EndOfStream)
{
    current_line = process.StandardOutput.ReadLine();
    if (current_line == "1")
    {
        // command is done executing 
        break;
    }

    OutputList.Add(current_line);
}

// contains all of the out put of the command
OutputList