在ASP.NET中運行控制台程式

來源:互聯網
上載者:User

解決一個問題後,最好的慶祝方式就是寫一篇部落格。今天解決了一個小問題,小小慶祝一下。

這個小問題是什嗎?

答:如何在ASP.NET調用MsBuild編譯一個VS2010解決方案?

是什麼需求帶來的這個問題?

答:發布一個網站的新版本時,通過瀏覽器觸發Web伺服器從Git版本控制伺服器中取出最新代碼,然後調用MsBuild進行編譯。。。

如何解決這個小問題的:

答:在ASP.NET中通過System.Diagnostics.Process運行msbuild.exe。

代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ 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";
//以release方式編譯CNBlogsJob.sln
ProcessStartInfo info = new ProcessStartInfo(filePath, @" E:\Dev\CNBlogsJob.sln /p:Configuration=release");
//將控制台輸出重新導向至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>

瀏覽器中顯示的結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.