.net – Is it possible to start / stop an application pool or website in IIS programatically

.netapplication-pooliisweb

I'm trying to find a way to stop, start, restart/recycle websites and application pools from within a web interface on the same server. Ideally this would be something I could do with .NET without having to execute shell commands.

(I see other related questions have been asked but I don't want to do this from the command line unless that's necessary).

I need to be able to do this for both IIS6 and IIS7.

Best Solution

You are looking for the Microsoft.Web.Administration.ServerManager class. You can find some starter code here: http://learn.iis.net/page.aspx/226/microsoftwebadministration/.

You can explore that code further to do what you want. I believe the whole appcmd is written on top of this framework.

This framework will not work for IIS6 and below. You need IIS7. For II6 you could use the WMI approach as suggested by Mehmet Aras. It is unfortunate, but you need these two libraries; or maybe there is some code out there that abstracts these libraries.