'How do you enter something at a console prompt programmatically?
I have program, that must interact with a console program before my program can continue what it is doing. I'm trying to avoid my user from having to interact with this dos program. So, I created a .bat
file that does everything I need to do except for the last step which still requires user interaction that I'm trying to avoid.
Specifically, the command I type ends up at a prompt where I need to automatically enter y
and then Enter (to say yes to the prompt) and then I want to exit out.
Is there any way that I can make this happen automatically without my user having to enter y
and Enter? Ideally, I'd like to have the console window NOT even pop up while this is going on.
Solution 1:[1]
The post from Microsoft also clearly says :
Do not type a space between the "y" and the pipe symbol (|)
and indeed, I noticed that in my case
echo y | executable.exe
doesn't work while
echo y| executable.exe
works fine
Solution 2:[2]
I used the following, since "echo y | executable.exe" didn't worked for me
// Write a "Y" to the process's input
proc.StandardInput.WriteLine("Y");
// Now that we've sent the confirmation "Y" wait for the process to exit
proc.WaitForExit();
as posted here: https://www.experts-exchange.com/questions/27024185/C-ProcessStart-How-to-automatically-press-the-Y-key.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | zigdapig |
Solution 2 | user6070921 |