JElster
asked on
Run a DOS batch file from C# and have C# press the Y and ENTER button
I need to run a dos batch file.. but at the end it prompts with Y and an ENTER.. to continue and overwrite a file.
Any ideas how I can have the Process pause and press the Y and Enter key??
TIA
ProcessStartInfo psi = new ProcessStartInfo(Configura tionSettin gs.AppSett ings["UpLo adLocation "]+"\\pgpR EGED.bat") ;
psi.UseShellExecute = true;
Process process = Process.Start(psi);
Any ideas how I can have the Process pause and press the Y and Enter key??
TIA
ProcessStartInfo psi = new ProcessStartInfo(Configura
psi.UseShellExecute = true;
Process process = Process.Start(psi);
ASKER
So if the process is asking for something how do I 'say' Y and press the Enter key?
thx
thx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It runs.. I get the black cmd window and it just waits.. and nothing happens until I close the cmd wind??
Any ideas?
thanks
ProcessStartInfo psi = new ProcessStartInfo(Configura tionSettin gs.AppSett ings["UpLo adLocation "]+"\\REGE D.bat");
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
Process process = Process.Start(psi);
string str = process.StandardOutput.Rea dToEnd();
process.StandardInput.Writ eLine("y") ; //and done :)
Any ideas?
thanks
ProcessStartInfo psi = new ProcessStartInfo(Configura
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput
psi.RedirectStandardError = true;
Process process = Process.Start(psi);
string str = process.StandardOutput.Rea
process.StandardInput.Writ
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Once it works, you may polish your app by using
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
to hide the black dos box.
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
to hide the black dos box.
Try This :
psi.RedirectStandardOutput = false;
psi.RedirectStandardError = false;
If something is goin wrong with the proggy itself we wont get wat it is saying. so dont redirect std out so u can see
if anything is goin wrong.
Regards,
Chinmay
psi.RedirectStandardOutput
psi.RedirectStandardError = false;
If something is goin wrong with the proggy itself we wont get wat it is saying. so dont redirect std out so u can see
if anything is goin wrong.
Regards,
Chinmay
ASKER
It's as thought the
process.StandardInput.Writ eLine("y") ; //and done :)
Doesn't work... Do I need to force an ENTER too?
thanks!
process.StandardInput.Writ
Doesn't work... Do I need to force an ENTER too?
thanks!
did you try flushing it? as I had mentioned in the last post
<Quote>
Try adding the following line after the writeline statement.
process.StandardInput.Flus h();
</Quote>
<Quote>
Try adding the following line after the writeline statement.
process.StandardInput.Flus
</Quote>
ASKER
Yes...
Just does do anything.. just hangs with the confirmation msg??
thx
Just does do anything.. just hangs with the confirmation msg??
thx
Hi There,
Try to put another process.StandardInput.Writ eLine();
If this doesn't work for you, can u pls. be spcific wat kind of batch you are running so we
can have better understaning of the scenario.
Regards,
Chinmay
Try to put another process.StandardInput.Writ
If this doesn't work for you, can u pls. be spcific wat kind of batch you are running so we
can have better understaning of the scenario.
Regards,
Chinmay
Dear JElster,
You may be happy to know that your process have been working. I am not sure why the output is not appearing. I tried similar thing here and got the same result. If you append the following code after the code that you already have, you should be able to see your output in the trace window
while (true)
{
string str = proc.StandardOutput.ReadLi ne();
if (str == null)
break;
Trace.WriteLine(str);
}
also modify the initial code to set RedirectStandardOutput to true.
Let us know how it goes.
You may be happy to know that your process have been working. I am not sure why the output is not appearing. I tried similar thing here and got the same result. If you append the following code after the code that you already have, you should be able to see your output in the trace window
while (true)
{
string str = proc.StandardOutput.ReadLi
if (str == null)
break;
Trace.WriteLine(str);
}
also modify the initial code to set RedirectStandardOutput to true.
Let us know how it goes.
Curry in a Hurry.... I am always in a hurry to click the submit button, anyways here's how my complete code looks :
ProcessStartInfo _psi = new ProcessStartInfo(@"d:\myfo lder\myfil e.bat");
_psi.WorkingDirectory = @"D:\myfolder";
_psi.UseShellExecute = false;
_psi.RedirectStandardInput = true;
_psi.RedirectStandardOutpu t = true;
_psi.RedirectStandardError = false;
_psi.Arguments = @"SP"; //my batch file takes an argument
Process proc = new Process();
proc.StartInfo = _psi;
proc.Start();
StreamWriter procWriter = proc.StandardInput;
procWriter.WriteLine("Y");
while (true)
{
string str = proc.StandardOutput.ReadLi ne();
if (str == null) //once there is nothing it returns null not an empty string
break;
Trace.WriteLine(str);
}
ProcessStartInfo _psi = new ProcessStartInfo(@"d:\myfo
_psi.WorkingDirectory = @"D:\myfolder";
_psi.UseShellExecute = false;
_psi.RedirectStandardInput
_psi.RedirectStandardOutpu
_psi.RedirectStandardError
_psi.Arguments = @"SP"; //my batch file takes an argument
Process proc = new Process();
proc.StartInfo = _psi;
proc.Start();
StreamWriter procWriter = proc.StandardInput;
procWriter.WriteLine("Y");
while (true)
{
string str = proc.StandardOutput.ReadLi
if (str == null) //once there is nothing it returns null not an empty string
break;
Trace.WriteLine(str);
}
ASKER
I'm trying to run PGP.exe (encrypts a file using PGP with a bunch of params)
It gives you a few message lines and some blank lines and then the confirmation (Y/N?)
When I run it with the code provided I get the first few message lines and then it just 'hangs'
any more ideas?
It gives you a few message lines and some blank lines and then the confirmation (Y/N?)
When I run it with the code provided I get the first few message lines and then it just 'hangs'
any more ideas?
1) In the process start info of the process, set the RedirectStandardOutput to true
2) after launching the process, do somthing like
string str = process.StandardOutput.Rea
3) Parse this string to see if the process is asking for something.
I have usually done it to parse the messages from the process after it has finished so can't provide exact code to you but this should definitely give you enough pointers to get going.
Regards,