C # execute BAT batch processing code in ASP. NET

Source: Internet
Author: User

Because the soft encryption service process of the company's internal network server may become stuck at an irregular time, you need to manually close the process and start it again. Every time a similar problem occurs, you always need to use a remote server or enter the data center to operate the machine, but sometimes it is not very convenient at night, but remote passwords and data center keys are not convenient for others, so think of the following solutions.
Create a BAT batch file to stop and start the service through stop/start, just as there is already IIS + on the server. NET2.0 writes An aspx page, allowing employees to access this page and click the bottom event to execute the BAT file.

Below is the C # source code

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 button#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 ('fix successful! '); </Script> ");*/

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

}

}

}


Author: YoungIvan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.