我有一个用Python制作的应用程序,它使用os.system([string])
现在我想将它从Python转移到某种语言,如ASP.NET或其他东西。
有没有办法访问服务器的命令提示符并使用ASP.NET或Visual Studio中的任何技术运行命令?
这需要在Web应用程序中运行,用户将单击该按钮,然后运行服务器端命令,因此所建议的技术与所有这些功能兼容非常重要。
答案 0 :(得分: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页面结合起来:
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>
然后是背后的代码:
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();
};
}
}
}