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>