aspx页面通过代码调用cmd来运行注册服务

时间:2008-07-07 04:23:31  来源:网翼  作者:  【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白

  之前不是写了windows服务吗,需要向系统注册服务,但是我只有ftp权限怎么办,不能远程到桌面。

  想了个办法,写了一个aspx页面,通过代码调用cmd来运行。当然,因为服务器安全放的比较开,内网吗~

  下面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CMD.aspx.cs" Inherits="Test_CMD" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 

<head runat="server"> 

<title>无标题页</title> 

</head> 

<body> 

<form id="form1" runat="server"> 

<div> 

<asp:Label ID="Label1" runat="server" Text="目录:"></asp:Label> 

<asp:TextBox ID="TextBox3" runat="server" Width="549px">c:\\</asp:TextBox> 

<br /> 

<asp:TextBox ID="TextBox1" runat="server" Height="113px" TextMode="MultiLine" Width="600px"></asp:TextBox> 

<br /> 

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="DO" /><br /> 

<asp:TextBox ID="TextBox2" runat="server" Height="150px" TextMode="MultiLine" Width="600px"></asp:TextBox></div> 

</form> 

</body> 

</html> 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CMD.aspx.cs" Inherits="Test_CMD" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 

<head runat="server"> 

<title>无标题页</title> 

</head> 

<body> 

<form id="form1" runat="server"> 

<div> 

<asp:Label ID="Label1" runat="server" Text="目录:"></asp:Label> 

<asp:TextBox ID="TextBox3" runat="server" Width="549px">c:\\</asp:TextBox> 

<br /> 

<asp:TextBox ID="TextBox1" runat="server" Height="113px" TextMode="MultiLine" Width="600px"></asp:TextBox> 

<br /> 

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="DO" /><br /> 

<asp:TextBox ID="TextBox2" runat="server" Height="150px" TextMode="MultiLine" Width="600px"></asp:TextBox></div> 

</form> 

</body> 

</html> 

     后台

<PRE class=csharp name="code">using System; 

using System.Data; 

using System.Configuration; 

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>


文章评论

共有 0位网翼网友发表了评论 查看完整内容