I've read lots of articles on how to use web setup projects and Custom Actions etc. However none of them seem to explain how to create new website in IIS that isn't under the 'Default Site'.
I've currently got the following coding, which has been adapted from one of the articles:
private const string MetaBasePath = "IIS://Localhost/W3SVC";
The following method is called during the Install method:
private void CreateWebsite()
{
DirectoryEntry IISWebSites = new DirectoryEntry(MetaBasePath);
DirectoryEntries sites = IISWebSites.Children;
DirectoryEntry TestAppWebSite = sites.Add("TestWebApp", (IISWebSites.SchemaClassName.Replace("Service", "Server")));
TestAppWebSite.Properties["ServerComment"][0] = "Testing";
TestAppWebSite.CommitChanges();
}
This seems to create a website but under the 'Default Site'. I'm pretty sure the 'MetaBasePath' is defined correctly as this is also used in another method to check if the website currently exists. This method goes through the children of the 'MetaBasePath' and checks the properties of each of them. I've stepped through this code and this is pulling back the websites I'd expect to see, so I know it's getting the right information from that path.
What am I missing?
UPDATE
I appear to be able to create a new site not under 'Default Site' if I use a site id rather than name.
As per my update for IIS 6 you can do this by using the site id. For this I got the site ids, ordered them and then looped through them. I then added 1 to give me the next 1.
For IIS 7 it's much easier. Just get the SiteCollection and add to that. See below:
private ServerManager mgr = new ServerManager();
SiteCollection sites = mgr.Sites;
int port;
Int32.TryParse(targetPort, out port);
string DirectoryPath = targetDirectory.Substring(0, targetDirectory.LastIndexOf("\\"));
site = sites.Add(targetSite, DirectoryPath, port);
mgr.CommitChanges();