tags:

views:

199

answers:

4

Well, just to keep it simple: I have a webform. On it a button called "Restart". I click on this button and IIS will restart itself.

Now, what would be the C# code that I would need to write behind the OnClick event of this web button? (If it's even possible?)


Then a second button is added. It's called "Reset" and should just reset the AppDomain for the current web application. What would be the code for this?

+7  A: 
protected void Reload_Click(object sender, EventArgs e)
{
    HttpRuntime.UnloadAppDomain();
}

protected void Restart_Click(object sender, EventArgs e)
{
    using (var sc = new System.ServiceProcess.ServiceController("IISAdmin"))
    {
        sc.Stop();
        sc.Start();
    }
}
Darin Dimitrov
This seems to be a good option as it will only restart your application and not the other applications hosted on that server.
Hemanshu Bhojak
+1  A: 

(http://www.velocityreviews.com/forums/t300488-how-can-i-restart-iis-or-server-from-aspx-page-or-web-service.html)

string processName = "aspnet_wp";

System.OperatingSystem os = System.Environment.OSVersion;

//Longhorn and Windows Server 2003 use w3wp.exe
if((os.Version.Major == 5 && os.Version.Minor > 1) || os.Version.Major ==6)
   processName = "w3wp";

foreach(Process process in Process.GetProcessesByName(processName))
   {
      Response.Write("Killing ASP.NET worker process (Process ID:" +
      process.Id + ")");
      process.Kill();
   }
Amadeus45
Be careful killing processes like this. It could have unexpected behaivours. Iisreset.exe will do all the taks needed to restart the iss, and it doesn't make a hard kill to the aspnet_wp for sure.
Ricardo
+3  A: 
Process iisreset = new Process();
iisreset.StartInfo.FileName   = "iisreset.exe";
iisreset.StartInfo.Arguments = "computer name";
iisreset.Start();

//iisreset.exe is located in the windows\system32 folder.
Ricardo
+1  A: 

Is there more than one web site hosted on the server where this code will run? If so, you may want to look at the System.DirectoryServices namespace, and restart the individual web site

There are several web sites hosted on this server, but the application that can do the reset is meant to reset them all. It's supposed to maintain those other sites.
Workshop Alex