Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove application from IIS7 c#

Tags:

c#

iis-7

web

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

like image 943
fireBand Avatar asked Dec 10 '22 09:12

fireBand


1 Answers

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).

like image 195
Kev Avatar answered Dec 11 '22 22:12

Kev