C# 如何从asp.net与命令行应用程序交互

C# 如何从asp.net与命令行应用程序交互,c#,asp.net,command-prompt,C#,Asp.net,Command Prompt,我在internet上找到一些代码,这些代码使我能够从ASP.Net网页向命令行发送命令。这适用于来自命令的1次响应,例如..“dir” 我遇到的问题是,如果我发送的命令返回一个响应,并期望我提供后续输入,该怎么办。例如,如果我有一个自定义控制台应用程序,我想从网页上与之交互,该怎么办。我该怎么做呢? 我包括了我找到的代码。注意,有一个文件“fine.bat”,它只包含命令“net user” 使用系统; 使用系统数据; 使用系统配置; 使用系统集合; 使用System.Web; 使用Syste

我在internet上找到一些代码,这些代码使我能够从ASP.Net网页向命令行发送命令。这适用于来自命令的1次响应,例如..“dir”
我遇到的问题是,如果我发送的命令返回一个响应,并期望我提供后续输入,该怎么办。例如,如果我有一个自定义控制台应用程序,我想从网页上与之交互,该怎么办。我该怎么做呢?
我包括了我找到的代码。注意,有一个文件“fine.bat”,它只包含命令“net user”

使用系统;
使用系统数据;
使用系统配置;
使用系统集合;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.HTMLControl;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(第IsPostBack页)
{
string exec=TextBox1.Text;
//获取完整的文件路径
字符串strFilePath=Server.MapPath(“fine.bat”);
//创建ProcessInfo对象
System.Diagnostics.ProcessStartInfo psi=新的System.Diagnostics.ProcessStartInfo(“cmd.exe”);
psi.UseShellExecute=false;
psi.0输出=真;
psi.INPUT=真;
psi.error=true;
//开始这个过程
System.Diagnostics.Process proc=系统.诊断.流程.启动(psi);
//打开批处理文件进行读取
//System.IO.StreamReader strm=System.IO.File.OpenText(strFilePath);
System.IO.StreamReader strm=proc.StandardError;
//连接输出以进行读取
System.IO.StreamReader sOut=过程标准输出;
//附上书面报告
System.IO.StreamWriter sIn=过程标准输入;
//将批处理文件的每一行写入标准输入
/*while(strm.Peek()!=-1)
{
sIn.WriteLine(strm.ReadLine());
}*/
犯罪记录(执行);
strm.Close();
//退出CMD.EXE
字符串stEchoFmt=“#{0}运行成功。正在退出”;
sIn.WriteLine(String.Format(stEchoFmt,strFilePath));
犯罪记录(“退出”);
//关闭流程
过程关闭();
//把单词读成字符串。
字符串结果=sOut.ReadToEnd().Trim();
//关闭io流;
sIn.Close();
sOut.Close();
//写出结果。
字符串fmtStdOut=“{0}”;
这个.Response.Write(“
”); 这个.Response.Write(“
”); 这个.Response.Write(“
”); this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine,“
”)); } } } 无标题页 >P>虽然它是从你的ASP.NET应用程序与控制台应用程序进行通信的,但你可能无法得到你所期望的结果,可能需要考虑一个或一个Web服务。

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

尽管它是从您的主机与控制台应用程序通信ASP.NET应用程序,您可能无法得到预期的结果,可能需要考虑一个或一个Web服务。
Microsoft不建议从Web应用程序调用.exe,因为出于安全原因,w3wp.exe在沙盒环境中运行,因此它启动的任何线程/任务/进程与您自己启动时的线程/任务/进程不同,因此可能无法按预期工作。

您有此控制台应用程序的源代码吗?那么直接从web服务调用它将是一场架构噩梦。控制台应用程序做什么工作?是否存在可以移动到web应用程序可以使用的类库中的逻辑?或者您可以将该控制台应用程序转换为Windows服务,并通过某种形式的进程间通信(如Web API调用或服务总线消息)与之通信吗?您有此控制台应用程序的源代码吗?那么直接从web服务调用它将是一场架构噩梦。控制台应用程序做什么工作?是否存在可以移动到web应用程序可以使用的类库中的逻辑?或者您可以将该控制台应用程序转换为Windows服务,并通过某种形式的进程间通信(如Web API调用或服务总线消息)与之通信吗?
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>