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