0

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>
Eminem
  • 7,206
  • 15
  • 53
  • 95
  • 1
    You have the source code for this console app? Then calling it directly from the web service is going to be an architecture nightmare. What work does the console app do? Is there logic that could be moved into a class library that the web application can consume? Or could you turn that console application into a Windows Service, and communicate with it via some form of interprocess communication, such as Web API calls or service bus messages? – mason Mar 14 '18 at 17:17

1 Answers1

2

Although it is possible to communicate with a console app from your ASP.NET app, you may not get the results you expect and might want to consider a task scheduler or a Web Service.

Microsoft does not recommend calling an .exe from a Web application as w3wp.exe runs in a sandboxed environment for security reasons and hence any thread/task/process that it launches is not the same as it would be when you launch it yourself and hence may not work as expected.

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91