HELP!! ADSI C# create virtual directory
Here is the problem I want to create web directories from a webpage. Here is the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using System.IO;
//Add reference to DirectoryServices
public class SetupUtility
{
public SetupUtility()
{
}
public int CreateWebSite(string webSiteName, string pathToRoot)
{
return CreateWebSite(webSiteName, pathToRoot, false);
}
public int CreateWebSite(string webSiteName, string pathToRoot, bool createDir)
{
System.DirectoryServices.DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
// Searches IIS and finds an unused ID value for new web site
int siteID = 1;
foreach(DirectoryEntry e in root.Children)
{
if(e.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(e.Name);
if(ID >= siteID)
{
siteID = ID+1;
}
}
}
// Create the web site and set some default options
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
site.Invoke("Put", "ServerComment", webSiteName);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", ":80:");
site.Invoke("Put", "ServerState", 2);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", "Default.aspx");
site.Invoke("Put", "SecureBindings", ":443:");
site.Invoke("Put", "ServerAutoStart", 1);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
// Create application virtual directory and set options
DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
siteVDir.Properties["Path"][0] = pathToRoot;
siteVDir.Properties["AccessScript"][0] = true;
siteVDir.CommitChanges();
siteVDir.Properties["AppFriendlyName"][0] = "Default Application";
siteVDir.Invoke("AppCreate3", new object[] {2, "DefaultAppPool", true});
siteVDir.CommitChanges();
return siteID;
}
}
I am calling it like so:
SetupUtility su = new SetupUtility ();
su.CreateWebSite(TextBox1.Text, "C:\\inetpub\\wwwroot\\" + TextBox1.Text, true);
System.IO.Directory.CreateDirectory("C:\\inetpub\\wwwroot\\" + TextBox1.Text);
OurLabel.Text += "Virtual Directory for " + TextBox1.Text + " was created" + "
";
It creates a new virtual Directory not under the Default WebSite. I want it to put it under the Default WebSite not on its own directory, how can I do this??