onfiguration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.IO;
using System.Text;
public partial class Test_CMD : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = Cmd(TextBox1.Text);
}
private string Cmd(string strExec)
{
string rl;
StringBuilder sb = new StringBuilder();
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = TextBox3.Text;//"c:\\";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(strExec);
p.StandardInput.WriteLine("exit");
while ((rl = p.StandardOutput.ReadLine()) != null)
{
sb.Append(rl + "\r\n");
//Response.Write(p.StandardOutput.ReadLine());
}
return sb.ToString();
}
}
</PRE>
<PRE class=csharp name="code">路径里面指定cmd中的路径。</PRE>
<PRE class=csharp name="code">因为每次运行完最好都exit一下,所以就不能使用过多命令,干脆直接指定初始时的路径了。</PRE>
<PRE class=csharp name="code">如果不exit得话也可以,但是一定想的关闭cmd,不然服务器当机就挂了。</PRE>
<PRE class=csharp name="code"> </PRE>
<PRE class=csharp name="code">接下来不需要我说了吧。</PRE>
<PRE class=csharp name="code">轻松搞定。
</PRE>