C#ASP.NET执行BAT批处理代码
? ?因公司內(nèi)網(wǎng)服務(wù)器的軟加密服務(wù)進(jìn)程會(huì)不定時(shí)的卡死,需要手工關(guān)閉進(jìn)程再啟動(dòng),每次出現(xiàn)類(lèi)似問(wèn)題總要用遠(yuǎn)程服務(wù)器或者進(jìn)入機(jī)房操作機(jī)器,但有時(shí)是晚上不是很方便,但遠(yuǎn)程的密碼和機(jī)房鑰匙又不方便給他人,因此想到以下解決方法。
??? 做一個(gè)BAT批處理文件來(lái)通過(guò)stop/start來(lái)關(guān)閉和啟動(dòng)服務(wù),正好服務(wù)器上已有IIS+.NET2.0,就寫(xiě)了一個(gè)aspx頁(yè)面,讓員工發(fā)現(xiàn)無(wú)法使用時(shí),通過(guò)訪問(wèn)該網(wǎng)頁(yè)點(diǎn)擊bottom事件執(zhí)行BAT文件來(lái)解決。
??? 以下是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('修復(fù)成功!'); </script>");*/
??? /*Response.Redirect("Success.html");*/
??? }
??? }
??? }
轉(zhuǎn)載于:https://blog.51cto.com/3403450/953702
總結(jié)
以上是生活随笔為你收集整理的C#ASP.NET执行BAT批处理代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基础概念总结(spring securi
- 下一篇: C# 属性反射