I had a requirement whereby I needed to test the availability of XML Web service and IIS restart was part of the requirement as well.
Below is the code for restarting IIS using c#.
using System.ServiceProcess;
using (ServiceController controller = new ServiceController())
{
controller.MachineName = “My local or remote computer name”;
controller.ServiceName = “IIS Service Name”; // i.e “w3svc”if (controller.Status != ServiceControllerStatus.Running)
{
// Start the service
controller.Start();Log.Debug(“IIS has been started successfully, now checking again for webservice availability”);
}
else
{
// Stop the service
controller.Stop();// Start the service
controller.Start();Log.Debug(“IIS has been restarted successfully”);
}
}