[英]Running Command line from an ASPX page, and returning output to page
我正在嘗試訪問命令行並執行命令,然后將輸出返回到我的aspx頁面。 一個很好的例子是在aspx頁面的頁面加載上運行dir並通過Response.Write()返回輸出。 我嘗試過使用下面的代碼。 當我嘗試調試它時,它會運行,但永遠不會完成加載,也不會呈現輸出。 我正在使用C#和.NET Framework 3.5sp1。 任何幫助非常感謝。
謝謝,布萊恩
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);
}
}
cmd.exe的命令行參數的語法有問題。 這就是為什么cmd永遠不會退出的原因。
為了讓cmd.exe運行程序然后退出,你需要發送語法“/ c [command]”。 嘗試使用該行運行相同的代碼
si.StartInfo.Arguments = "dir";
換成了
si.StartInfo.Arguments = "/c dir";
看看它是否有效
最有可能的問題是權限問題。 運行ASP.NET進程的用戶權限非常有限。
因此,要么必須為該用戶設置適當的權限,要么在其他用戶下運行ASP.NET。
這隱藏了安全風險,所以你必須非常小心。
這太瘋狂了! 使用System.IO名稱空間從C#程序中創建文件列表! 這很容易做到; 雖然這種技術也有授權問題。
使用System.Diagnostics.Process。
下面是一些ASP.NET代碼,用於在命令行上運行subversion命令。
///////////////////////////////////////////////////////////////////////
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.