Pass input to a running process.

  • Thread starter Thread starter Marc Jennings
  • Start date Start date
M

Marc Jennings

Hi all,

I want to run an external app from an assembly. Simple enough...
Process ExtProcess = new Process();
ExtProcess.StartInfo.FileName = myAppName;
ExtProcess.StartInfo.Arguments = myArgs;
...etc...

My problem is that the command I am cfalling produces an "Are you
sure?" message that is not overridable with a switch (eg. /y)

Is there any way for the framework to wait for the "Are you sure?"
string, and then send a response (in this case, "y")?

TIA
Marc.
 
Marc,

What you could do is wait until a time you know that the application
will put up the message box, and then use the SendInput (through the
P/Invoke layer) to simulate the mouse click. Of course, you will have to
determine some way to find out when that message box is shown, and then send
the input. All in all, it's very hokey, but there is little you can do
about it, it would seem.

Hope this helps.
 
I found a workaround (as always happens just after I post a question,
never before...)

In this case, I am calling cacls.exe to set ACL on a file / folder.
Process ExtProcess = new Process();
ExtProcess.StartInfo.FileName = "cmd";
ExtProcess.StartInfo.Arguments = "/C echo Y| cacls /E /R user:F");

works a treat.

Thanks.
Marc.
 
Marc,
I want to run an external app from an assembly.
Is there any way for the framework to wait for the "Are you sure?"
string, and then send a response (in this case, "y")?

Is your process a console (text-mode) application? If so, you could use
something called "standard input/output redirection".

That is, .NET gives you access to the streams for input and out of the
console process (what it reads in and what it writes out), using the
StandardInput and StandardOutput properties of the Process class. If you
look at the .NET SDK Documentation for the StandardOutput property, you can
also find a simple example.

The idea is that you read the standard output until you find the "Are you
sure?" string, and then write the letter "y" (probably along with the Enter
key) to the standard input stream of the process.

That should solve your problem. GUI applications are a totally different
thing.

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Since it is a command line program, you can also redirect the standard
input and output and watch for the message and then send the response
back.
 
Back
Top