C # ASP. NET controls enabling, disabling, and restarting windows Services

Source: Internet
Author: User

Use ASP. NET to enable and Disable Windows Services
Effect

Code
Reference must be added to the home page.

In pageload of the page
Instantiate a windows Service
Protected void Page_Load (object sender, EventArgs e)
{
// The code is used to display the page loading effect, which can be ignored.
ClientScript. RegisterClientScriptBlock (GetType (), "", initJavascript ());
// Instantiate the windows service. LedIPTransfer is a windows Service written by myself. You can use
Service = new ServiceController ("LedIPTransfer ");
OnLoad ();
}
To obtain the service name, view the service details and copy the display name.

Displays the status of the current service on the page.
Private void OnLoad ()
{
// Obtain the service running status
String state = service. Status. ToString ();
Switch (state)
{
Case "Stopped ":
LblState. Text = "Service stopped ";
BtnRestart. Enabled = false;
BtnStart. Enabled = true;
BtnStop. Enabled = false;
Break;
Case "Running ":
LblState. Text = "Service Running ";
BtnStart. Enabled = false;
BtnRestart. Enabled = true;
Break;
/* The following service statuses are for reference only. Unless the service is started slowly, you generally do not need to set them as follows:
Case "Paused ":
LblState. Text = "service suspended ";
Break;
Case "StartPending ":
LblState. Text = "the service is starting ";
Break;
Case "StopPending ":
LblState. Text = "the service is stopping ";
Break;
Case "ContinuePending ":
LblState. Text = "the service is about to continue ";
Break;
Case "PausePending ":
LblState. Text = "the service is about to be suspended ";
Break;
**/
}
}
/// <Summary>
/// Start the service
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protectedvoid btnStart_Click (object sender, EventArgs e)
{
Service. Start ();
// Wait until the service reaches the running status
Service. WaitForStatus (ServiceControllerStatus. Running );
ClientScript. RegisterStartupScript (GetType (), "", "window. location. href = window. location. href", true );
}
/// <Summary>
/// Stop the service
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protectedvoid btnStop_Click (object sender, EventArgs e)
{
Service. Stop ();
Service. WaitForStatus (ServiceControllerStatus. Stopped );
ClientScript. RegisterStartupScript (GetType (), "", "window. location. href = window. location. href", true );
}
/// <Summary>
/// Restart the service
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protectedvoid btnRestart_Click (object sender, EventArgs e)
{
// Stop the service
Service. Stop ();
Service. WaitForStatus (ServiceControllerStatus. Stopped );
// Start the service
Service. Start ();
Service. WaitForStatus (ServiceControllerStatus. Running );
ClientScript. RegisterStartupScript (GetType (), "", "window. location. href = window. location. href", true );
}

ASP. NET requires high permissions to enable services. Therefore, you cannot enable services if you have insufficient permissions.
You can add identiy under system. web to simulate that the user has administrator permissions.
<System. web>
<Identity impersonate = "true" userName = "userName" password = "password"/>
</System. web>

Related Article

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.