2008-10-29 86 views
10

我試圖訪問命令行並執行命令,然後將輸出返回到我的aspx頁面。一個很好的例子是在頁面加載一個aspx頁面並通過Response.Write()返回輸出結果。我曾嘗試使用下面的代碼。當我嘗試調試它時,它會運行,但從未完成加載並且不顯示輸出。 我正在使用C#和.NET Framework 3.5sp1。任何幫助非常感謝。從ASPX頁面運行命令行,並返回輸出到頁面

感謝, 布萊恩

public partial class CommandLine : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     System.Diagnostics.Process si = new System.Diagnostics.Process(); 
     si.StartInfo.WorkingDirectory = @"c:\"; 
     si.StartInfo.UseShellExecute = false; 
     si.StartInfo.FileName = "cmd.exe"; 
     si.StartInfo.Arguments = "dir"; 
     si.StartInfo.CreateNoWindow = true; 
     si.StartInfo.RedirectStandardInput = true; 
     si.StartInfo.RedirectStandardOutput = true; 
     si.StartInfo.RedirectStandardError = true; 
     si.Start(); 
     string output = si.StandardOutput.ReadToEnd(); 
     si.Close(); 
     Response.Write(output); 
    } 
} 

回答

9

您對cmd.exe的命令行參數的語法有問題。這就是爲什麼cmd永遠不會退出。
爲了讓cmd.exe運行一個程序然後退出,您需要發送語法「/ c [command]」。嘗試用

 si.StartInfo.Arguments = "/c dir"; 

取代了線

 si.StartInfo.Arguments = "dir"; 

運行相同的代碼,看看它是否工作。

+0

非常感謝!很棒。我實際上使用w/perforce進行交互。 – user32474 2008-10-29 18:54:33

3

最有可能你的問題與權限。運行ASP.NET進程的用戶權限非常有限。

因此,您必須爲該用戶設置適當的權限,或者在某個其他用戶下運行ASP.NET。

雖然這隱藏了安全隱患,所以您必須非常小心。

+1

如果權限是問題,程序不會掛起 - 它會導致異常。他說的是程序掛起而且從未完成運行。 – configurator 2008-10-29 17:33:35

0

這是瘋了!使用System.IO命名空間從C#程序中創建文件列表!這很容易做到;雖然這種技術也有授權問題。

+1

我認爲這只是一個例子,並不是真正的預期用途。 – 2008-10-29 17:45:49

+1

這讓我發笑 - 敏銳! – pfeds 2013-09-26 02:29:04

0

使用System.Diagnostics.Process。

下面是一些ASP.NET代碼在shell命令行上運行顛覆命令的情況。

/////////////////////////////////////////////////////////////////////// 
    public static string run_svn(string args_without_password, string svn_username, string svn_password) 
    { 
     // run "svn.exe" and capture its output 

     System.Diagnostics.Process p = new System.Diagnostics.Process(); 
     string svn_path = Util.get_setting("SubversionPathToSvn", "svn"); 
     p.StartInfo.FileName = svn_path; 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.RedirectStandardError = true; 

     args_without_password += " --non-interactive"; 
     Util.write_to_log ("Subversion command:" + svn_path + " " + args_without_password); 

     string args_with_password = args_without_password; 

     if (svn_username != "") 
     { 
      args_with_password += " --username "; 
      args_with_password += svn_username; 
      args_with_password += " --password "; 
      args_with_password += svn_password; 
     } 

     p.StartInfo.Arguments = args_with_password; 
     p.Start(); 
     string stdout = p.StandardOutput.ReadToEnd(); 
     p.WaitForExit(); 
     stdout += p.StandardOutput.ReadToEnd(); 

     string error = p.StandardError.ReadToEnd(); 

     if (error != "") 
     { 
      Util.write_to_log(error); 
      Util.write_to_log(stdout); 
     } 

     if (error != "") 
     { 
      string msg = "ERROR:"; 
      msg += "<div style='color:red; font-weight: bold; font-size: 10pt;'>"; 
      msg += "<br>Error executing svn.exe command from web server."; 
      msg += "<br>" + error; 
      msg += "<br>Arguments passed to svn.exe (except user/password):" + args_without_password; 
      if (error.Contains("File not found")) 
      { 
       msg += "<br><br>***** Has this file been deleted or renamed? See the following links:"; 
       msg += "<br><a href=http://svn.collab.net/repos/svn/trunk/doc/user/svn-best-practices.html>http://svn.collab.net/repos/svn/trunk/doc/user/svn-best-practices.html</a>"; 
       msg += "<br><a href=http://subversion.open.collab.net/articles/best-practices.html>http://subversion.open.collab.net/articles/best-practices.html</a>"; 
       msg += "</div>"; 
      } 
      return msg; 
     } 
     else 
     { 
      return stdout; 
     } 
    }