如何与asp.net中的命令行应用程序进行交互

时间:2018-03-14 16:19:30

标签: c# asp.net command-prompt

我在互联网上找到了一些代码,这些代码使我能够从ASP.Net网页向命令行发送命令。这适用于来自命令的一次响应,例如......" dir"
我遇到的问题是,如果我发送的命令发回一个响应,并期望我的后续输入。例如,如果我有一个自定义控制台应用程序,我想从网页进行交互。我该怎么做呢? 我已经包含了我找到的代码。注意有一个文件" fine.bat"它只包含命令" 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>

1 个答案:

答案 0 :(得分:2)

虽然从ASP.NET应用程序与控制台应用程序进行通信是possible,但您可能无法获得预期的结果,并且可能需要考虑task scheduler或Web服务。

Microsoft不建议从Web应用程序调用.exe,因为出于安全原因w3wp.exe在沙盒环境中运行,因此它启动的任何线程/任务/进程与启动它时不同你自己,因此可能无法按预期工作。