C#程序调用,互动与标准输入和标准输出 [英] C# Process Call, Interact with Standard Input and Standard Output
问题描述
我有一个按钮,我点击了执行命令。该命令可能会弹出一些标准输入,我需要到输入响应,问题是这样的程序运行可能每天都在,所以我需要跨preT标准输出,并相应地重定向标准输入不同。
I have a button I click that executes a command. The command may prompt for some standard input and I need to respond to that input, the problem is the way the program runs may be different from day to day so I need to interpret the standard output and redirect standard input accordingly.
我有这样一段简单的code表示逐行读取标准输出线,当它看到密码的提示,它会发送标准输入,但该程序只是挂起,因为它从来没有看到提示为密码,但是当我运行该批处理文件中的密码提示是存在的。
I have this simple piece of code that reads the standard output line by line and when it sees a prompt for password, it'll send standard input, however the program just hangs since it never sees the prompt for the password, but when I run the batch file the password prompt is there.
下面是批处理文件我打电话来执行此测试:
Here is the batch file I am calling to execute this test:
@echo off
echo This is a test of a prompt
echo At the prompt, Enter a response
set /P p1=Enter the Password:
echo you entered "%p1%"
下面是批处理文件的命令行中运行时的输出:
Here is the output of that batch file when run from the command line:
C:\Projects\SPP\MOSSTester\SPPTester\bin\Debug>test4.bat
This is a test of a prompt
At the prompt, Enter a response
Enter the Password: Test1
you entered "Test1"
下面是C#代码片断我用来调用挂的批处理文件:
Here is the C# snippet I am using to call the batch file that is hanging:
var proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c test4.bat";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
//read the standard output and look for prompt for password
StreamReader sr = proc.StandardOutput;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
Debug.WriteLine(line);
if (line.Contains("Password"))
{
Debug.WriteLine("Password Prompt Found, Entering Password");
proc.StandardInput.WriteLine("thepassword");
}
}
sr.Close();
proc.WaitForExit();
下面是调试标准输出我看到,通知,我从来没有看到提示输入密码,这是为什么?它只是挂?</ P>
Here is the debug standard output I see, notice that I never see the prompt for the password, why is this? it just hangs?
This is a test of a prompt
At the prompt, Enter a response
有没有一种方法可以让我看的提示标准输出,并作出相应的反应呢?
Is there a way I can watch the standard output for prompting and react to it accordingly?
推荐答案
您的主要问题是,你是通过流阅读器上使用工作 sr.ReadLine()
这是一个问题,因为提示输入密码,暂停直至用户输入新线(输入密码后点击进入)。
Your main problem is that you are working through the stream reader using sr.ReadLine()
this is an issue because the prompt for a password halts until the user enters a new line (hits enter after typing the password).
所以,你需要读取流1个字符的时间。这个例子应该可以帮助您开始。
So you need to read the stream 1 character at a time. This example should help you get started.
while (!sr.EndOfStream)
{
var inputChar = (char)sr.Read();
input.Append(inputChar);
if (StringBuilderEndsWith(input, Environment.NewLine))
{
var line = input.ToString();
input = new StringBuilder();
Debug.WriteLine(line);
}
else if (StringBuilderEndsWith(input, "Password:"))
{
Debug.WriteLine("Password Prompt Found, Entering Password");
proc.StandardInput.WriteLine("thepassword");
var line = input.ToString();
input = new StringBuilder();
Debug.WriteLine(line);
}
}
private static bool StringBuilderEndsWith(StringBuilder haystack, string needle)
{
var needleLength = needle.Length - 1;
var haystackLength = haystack.Length - 1;
for (int i = 0; i < needleLength; i++)
{
if (haystack[haystackLength - i] != needle[needleLength - i])
{
return false;
}
}
return true;
}
这篇关于C#程序调用,互动与标准输入和标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!