asp.net中调用命令行

时间:2023-12-28 11:58:44

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<%@ Import Namespace="System.Diagnostics" %>
<script runat="server">
protected override void OnInit(EventArgs e)
{
string filePath = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
string param = @" E:\Dev\CNBlogsJob.sln /p:Configuration=release";

filePath = "notepad.exe";
param = "";

//以release方式编译CNBlogsJob.sln
ProcessStartInfo info = new ProcessStartInfo(filePath, param);
//将控制台输出重定向至StandardOutput,如果为false,就无法得到控制台输出结果
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
//下面一行代码是为了模拟控制台的显示效果
Response.Write("<body style=\"color:#DDD;background-color:#000;\">");
//控制台输出结果
Response.Write(p.StandardOutput.ReadToEnd().Replace("\n","<br/>"));
Response.Write("</body>");
p.WaitForExit();
p.Close();
}
</script>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>