Home > Net >  Removing Virtual Directory From IIS
Removing Virtual Directory From IIS

Time:12-27

I have one website as you see below.

I am trying to remove my virtual path which is called "devtest".

IIS

I tried these code to remove but always "Application" comes null.

        private static void RemoveVirtualDirectory()
        {
            using (ServerManager mgr = new ServerManager())
            {

                Application app = mgr.Sites["deneme.com"].Applications["/devtest"];
                mgr.Sites["deneme.com"].Applications.Remove(app);
                mgr.CommitChanges();
            }
        }

Do you have a better way to delete virtual directory?

CodePudding user response:

you can also delete using the DeleteVirtualDirectory method.

using (ServerManager mgr = new ServerManager())
{
    Site site = mgr.Sites["deneme.com"];
    if (site != null)
    {
        Application app = site.Applications["/devtest"];
        if (app != null)
        {
            app.DeleteVirtualDirectory();
            mgr.CommitChanges();
        }
    }
}

CodePudding user response:

I solved like below:

        private static void RemoveVirtualDirectory()
        {
            using (ServerManager mgr = new ServerManager())
            {
                Application app = mgr.Sites["test.com"].Applications["/"];
                var virtualDirectory = app.VirtualDirectories["/devtest"];
                app.VirtualDirectories.Remove(virtualDirectory);
                mgr.CommitChanges();
            }
        }
  • Related