当前位置:编程学习 > asp >>

C#ASP.NET执行BAT批处理代码

因公司内网服务器的软加密服务进程会不定时的卡死,需要手工关闭进程再启动,每次出现类似问题总要用远程服务器或者进入机房操作机器,但有时是晚上不是很方便,但远程的密码和机房钥匙又不方便给他人,因此想到以下解决方法。
做一个BAT批处理文件来通过stop/start来关闭和启动服务,正好服务器上已有IIS+.NET2.0,就写了一个aspx页面,让员工发现无法使用时,通过访问该网页点击bottom事件执行BAT文件来解决。

以下是C#源代码

 using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication3

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

           

            System.Diagnostics.ProcessStartInfo pro = new System.Diagnostics.ProcessStartInfo("cmd.exe");

            pro.UseShellExecute = false;

            pro.RedirectStandardOutput = true;

           

            pro.RedirectStandardError = true;

            pro.Arguments = "/K D:\\jiamiError\\test.bat";

            pro.WorkingDirectory = "D:\\jiamiError\\";

                      

            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(pro);

           

            System.IO.StreamReader sOut = proc.StandardOutput;

            proc.Close();           

            string results = sOut.ReadToEnd().Trim();

            sOut.Close();

            string fmtStdOut = "<font face=courier size=0>{0}</font>";

            this.Response.Write(String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>")));

            /*Response.Write("<script>alert('修复成功!'); </script>");*/

            /*Response.Redirect("Success.html");*/

        }

    }

}


作者:YoungIvan
补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,