2008-10-29 118 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; 
     } 
    }