Removing Virtual Directory From IIS

255 Views Asked by At

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?

2

There are 2 best solutions below

0
Mehmet On

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();
            }
        }
0
Vivek Nuna On

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();
        }
    }
}