简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM