Search This Blog

Tuesday, June 19, 2012

Restart IIS from asp net page

ASP.NET

ServiceController represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.
The Internet Information Services (IIS) World Wide Web Publishing Service (W3SVC) manages the HTTP protocol and HTTP performance counters.

Example :
Restart IIS from asp net page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceProcess;
using System.Threading;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceController sc = new ServiceController("W3SVC");
            if (null != sc)
            {
                if (sc.Status == ServiceControllerStatus.Running)
                {
                    sc.Stop();
                    Thread.Sleep(2000);
                    sc.Start();
                    Response.Write("IIS restarted");
                }
                else
                {
                    Response.Write("IIS started");
                    sc.Start();
                }
            }
        }
    }
}