I’ve made a page that allows users to view a list of batch files on a server, and run them.
The problem is when a batch file requires input such as “Press any key to continue” or “Press Y if you’re sure you want to continue”. Currently my code is providing input to preset situations:
if (outputLine.Contains("Y or N"))
inputLine = "Y";
if (outputLine.Contains("pause"))
inputLine = "X";
But I now want to give the user the ability to provide input instead of my code.
I’ve spent most of today researching how to do this and have come up with nothing.
Here’s my code:
private void RunScript(int scriptId)
{
ScriptData data = new ScriptData();
Script script = data.GetScript(scriptId);
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = script.Path,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = false
};
Process proc = new Process();
proc.StartInfo = psi;
proc.Start();
StreamReader reader = proc.StandardOutput;
StreamWriter writer = proc.StandardInput;
string outputLine;
string inputLine = string.Empty;
string display = string.Empty;
while (!reader.EndOfStream)
{
outputLine = reader.ReadLine();
display += outputLine + "\r\n";
if (inputLine != string.Empty)
display += inputLine + "\r\n";
inputLine = string.Empty;
if (outputLine.Contains("Y or N"))
inputLine = "Y";
if (outputLine.Contains("pause"))
inputLine = "X";
if (inputLine != string.Empty)
writer.WriteLine(inputLine);
}
display = Server.HtmlEncode(display);
display = display.Replace("\r\n", "<br />");
litOutput.Text = display;
}
Finally solved the problem, what I’ve got now when I run a script is very close to a command prompt window.
Here’s a very basic summary of how it works:
I start a new process that runs a batch file. At the same time I start a newthread that continuously loops asking the process for more output, and storing that output in a queue. My .NET timer continuously checks the queue for text, and outputs it into my ajax panel. I use a text box and ajax to input text into the process input and into my ajax panel. Also needed a lot of javascript and I had to use session variables to make it run smoothly.