Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Password / String to External Console Application C#

I am having need to automate an external windows console application from C#. Application is basically interface to an external device. When I invoke application it will ask me for authentication ie to enter a password with prompt something like 'Enter password:'. Right now there is no way to configure this application to run without interactive password prompt.

So I want to automate same from C# by sending password whenever it prompts and then to fire come commands which will execute on external device and then grab output. I know about process class and I am having some pointers like I can use pipes for this purpose ( Not Sure ? ).

As I have not handled this kind of automation before I am looking for help / direction form this.

Thanks in Advance.

like image 834
Akshay Avatar asked Jun 12 '26 23:06

Akshay


1 Answers

The way to do this is to use the Redirection members, e.g ProcessStartInfo.RedirectStandardInput Property

     Process myProcess = new Process();

     myProcess.StartInfo.FileName = "someconsoleapp.exe";
     myProcess.StartInfo.UseShellExecute = false;
     myProcess.StartInfo.RedirectStandardInput = true;
     myProcess.StartInfo.RedirectStandardOutput = true;
     myProcess.StartInfo.ErrorDialog = false;

     myProcess.Start();

     StreamWriter stdInputWriter  = myProcess.StandardInput;
     StreamReader stdOutputReader  = myProcess.StandardOutput;

     stdInputWriter.WriteLine(password);

     var op = stdOutputReader.ReadLine();

     // close this - sending EOF to the console application - hopefully well written
     // to handle this properly.
     stdInputWriter.Close();


     // Wait for the process to finish.
     myProcess.WaitForExit();
     myProcess.Close();
like image 167
Richard Harrison Avatar answered Jun 14 '26 13:06

Richard Harrison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!