fireBand
fireBand

Reputation: 957

Remove application from IIS7 c#

I am trying to remove an application from the default web site in IIS7 during uninstallation. Here's my code which does not work:

Microsoft.Web.Administration.ServerManager iisManager;
iisManager = new Microsoft.Web.Administration.ServerManager();
Microsoft.Web.Administration.Site defaultSite;
defaultSite = iisManager.Sites["Default Web Site"];
Microsoft.Web.Administration.Application myApplication ;
myApplication = defaultSite.Applications["MyApplication"];

defaultSite.Applications.Remove(myApplication );

iisManager.CommitChanges();

What is the right way to do this?

Thanks

Upvotes: 6

Views: 7763

Answers (1)

Kev
Kev

Reputation: 119856

This should do the trick:

using (ServerManager serverManager = new ServerManager())
{
    Site site = serverManager.Sites["Default Web Site"];
    Application application =  site.Applications["/MyApplication"];
    site.Applications.Remove(application);
    serverManager.CommitChanges();
}

The code does make the presumption that you are deleting the application /MyApplication from the root of the Default Web Site (IIS number #1).

Upvotes: 16

Related Questions