【问题标题】:ASP.NET equivalent to Python's os.system([string])ASP.NET 等价于 Python 的 os.system([string])
【发布时间】:2013-06-04 17:01:12
【问题描述】:

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

现在我想把它从 Python 中转移到某种语言中,比如 ASP.NET 之类的。

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

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

【问题讨论】:

标签: python asp.net visual-studio-2010 windows-server-2012


【解决方案1】:

它不是特定于 ASP.net 的,而是在 c# 中:

using System.Diagnostics;

Process.Start([string]);

或者对运行程序的特定部分(如参数和输出流)有更多访问权限

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();

您可以通过以下方式将其与 ASPx 页面相结合:

第一个进程.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>

然后是后面的代码:

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();
            };
        }
    }
}

【讨论】:

  • 这整个事情对我来说都是未知的水域,但是有没有办法可以在 Web 应用程序中运行这个 C# 代码?例如,用户点击某物,然后触发此代码。
  • 在 Web 应用程序中,如果您想访问页面以触发此代码,则此代码将附加到 Page_Load() 函数后面的代码中,或者如果您愿意,则附加到“_OnClick()”事件中点击&lt;ASP:Button&gt;时触发它
  • 好的,我相信你。谢谢。顺便说一句,有任何新手教程可以帮助我熟悉 asp.net 和 c# 脚本吗?我也会研究,但我想我会问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2021-01-02
相关资源
最近更新 更多