Page

Start or Stop IIS Service in C#,using Asp.net

If you want to start or stop IIS service in c#, vb.net using asp.net we need to write the code like as shown below

protected void btnClick_Click(object sender, EventArgs e)
{
ServiceController controller = new ServiceController("W3SVC");
if (controller.Status == ServiceControllerStatus.Running)
controller.Stop();
else if (controller.Status == ServiceControllerStatus.Stopped)
controller.Start();
else
controller.Stop();

}


If you want to see it in complete example open your Default.aspx page and write the code like as shown below

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Start or Stop IIS service in Asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnClick" runat="server" Text="Check Status"
onclick="btnClick_Click" />
</div>
</form>
</body>

</html>


Now we need to add System.ServiceProcess reference to our application for that right click on your application à select Add Reference à select System.ServiceProcess like as shown below





Now write the following code in code behind to check the status of IIS manager


using System;
using System.ServiceProcess;

 public partial class Default : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
        {
        }
     protected void btnClick_Click(object sender, EventArgs e)
      {
       ServiceController controller = new ServiceController("W3SVC");
       if (controller.Status == ServiceControllerStatus.Running)
       controller.Stop();
       else if (controller.Status == ServiceControllerStatus.Stopped)
       controller.Start();
     else
         controller.Stop();
       }
}


No comments:

Post a Comment