0

我有一个用 Python 制作的应用程序,它使用以下命令访问 Linux 服务器的命令提示符 os.system([string])

现在我想把它从 Python 转移到像 ASP.NET 之类的语言中。

有没有办法访问服务器的命令提示符并使用 ASP.NET 或 Visual Studio 中的任何技术运行命令?

这需要在 Web 应用程序中运行,用户将在其中单击一个按钮,然后运行服务器端命令,因此建议的技术与所有这些兼容是很重要的。

4

1 回答 1

1

Well it isn't ASP.net specific but in c#:

using System.Diagnostics;

Process.Start([string]);

Or With more access to the specific parts of running a program (like arguments, and output streams)

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c dir *.cs";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();

here is how you could combine this with an ASPx Page:

First Process.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Process.aspx.cs" Inherits="com.gnld.web.promote.Process" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Test Process</title>
    <style>
        textarea { width: 100%; height: 600px }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:Button ID="RunCommand" runat="server" Text="Run Dir" onclick="RunCommand_Click" />
      <h1>Output</h1>
      <asp:TextBox ID="CommandOutput" runat="server" ReadOnly="true" TextMode="MultiLine" />
    </form>
  </body>
</html>

Then the code behind:

using System;

namespace com.gnld.web.promote
{
    public partial class Process : System.Web.UI.Page
    {
        protected void RunCommand_Click(object sender, EventArgs e)
        {
            using (var cmd = new System.Diagnostics.Process()
            {
                StartInfo = new System.Diagnostics.ProcessStartInfo()
                {
                    FileName = "cmd.exe",
                    Arguments = "/c dir *.*",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true
                }
            })
            {
                cmd.Start();
                CommandOutput.Text = cmd.StandardOutput.ReadToEnd();
            };
        }
    }
}
于 2013-06-04T17:07:06.903 回答