I am currently creating a console application to interact with git. This works fine with redirecting the output of my command to the console application but now some commands prompt for inputs like PassPhrase or y/n for owerwrite. How to redirect these prompts and ask for inputs from the user.
var process = new Process();
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = workDir;
process.StartInfo.FileName = appName;
process.StartInfo.Arguments = argument;
process.Start();
var output = new List<string>();
while (process.StandardOutput.Peek() > -1)
{
output.Add(process.StandardOutput.ReadLine());
}
while (process.StandardError.Peek() > -1)
{
output.Add(process.StandardError.ReadLine());
}
process.WaitForExit();
foreach (var opt in output)
{
Console.WriteLine(opt);
}
You need to read output and error asynchronously; this will both make it possible for you to read input from the user in the meantime, and avoid a hang when the buffer for standard error gets full (you're only reading it after standard output ends, which is not enough).
Processes don't ask for input; they just read the input stream (when using standard input, of course). The simplest way to take care of this is to keep reading standard input on your side, and posting the received data to the standard input of your "hosted" process.