I found some code on the internet which enables me to send commands to the command line from ASP.Net web page. This works for 1 time responses from commands e.g.. "dir"
The issue i have is what if the command I send sends back a response and expects subsequent input from me. For example what if I have a custom console app I want to interact with from the web page. How would I go about doing that?
Ive included the code that I found. Note there is a file "fine.bat" which simply contains the command "net user"
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
string exec = TextBox1.Text;
// Get the full file path
string strFilePath = Server.MapPath("fine.bat");
// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
// Open the batch file for reading
//System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
System.IO.StreamReader strm = proc.StandardError;
// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;
// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;
// Write each line of the batch file to standard input
/*while(strm.Peek() != -1)
{
sIn.WriteLine(strm.ReadLine());
}*/
sIn.WriteLine(exec);
strm.Close();
// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";
sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");
// Close the process
proc.Close();
// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();
// Close the io Streams;
sIn.Close();
sOut.Close();
// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write("<br>");
this.Response.Write("<br>");
this.Response.Write("<br>");
this.Response.Write(String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>")));
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 11px; position: absolute;
top: 7px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Style="z-index: 102; left: 184px; position: absolute;
top: 7px" Text="Run" />
</div>
</form>
</body>
</html>