Creating Virtual Directory programmatically with IIS8.0

718 views Asked by At

I am trying to create a Virtual Directory progeammatically using the below code:

ServerManager iisManager = new ServerManager();
iisManager.Sites.Add("NewSite", "http", "*:8080:", @"D:\mine\TestApps\TestAppXML");
iisManager.CommitChanges();

this code is working fine when using through Console application. but when I am using the same code through Web Application, it is not doing the desired job. Nor does it give any error. Anyone has ideas about why the same code is not working with Web Application?

1

There are 1 answers

0
pool pro On

I dont see were you are grabing the website id.

The way to do this is to manipulate the Site.Applications collection which is a flattened tree of all the applications in your site.

If you would like to create a virtual directory try this:

//Get site information
Site site = srvam.Sites.First(s => s.Id == 3);
if (!SiteExists(srvman, siteId))
                    throw new ApplicationException();
Application iisManager = site.Applications.First(a => a.Path == "/");
iisManager.VirtualDirectories.Add("/vdir_1", @"D:\MySite\other_content");
srvam.CommitChanges();

If you would like to create a virtual Application use this code:

//Get site information
    Site site = srvman.Sites.First(s => s.Id == 3);
if (!SiteExists(srvman, siteId))
                    throw new ApplicationException();
Application app = 
   site.Applications.Add("/app_2", @"d:\mysite\other_content");
app.ApplicationPoolName = "MySite";
srvman.CommitChanges();

There is a great write up here