private static void CreateVDir(string metabasePath, string vDirName, string physicalPath)
{
// metabasePath is of the form "IIS:/Root[/]"
// for example "IIS://localhost/W3SVC/1/Root".
// vDirName is of the form "", for example, "MyNewVDir".
// physicalPath is of the form ":\", for example, "C:\Inetpub\Wwwroot".
Console.WriteLine("\nCreating virtual directory {0}/{1}, mapping the Root application to {2}:",
metabasePath, vDirName, physicalPath);
DirectoryEntry site = new DirectoryEntry(metabasePath);
string className = site.SchemaClassName;
if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
{
DirectoryEntries vdirs = site.Children;
DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace("Service", "VirtualDir")));
newVDir.Properties["Path"][0] = physicalPath;
newVDir.Properties["AccessScript"][0] = true;
// These properties are necessary for an application to be created.
newVDir.Properties["AppFriendlyName"][0] = vDirName;
newVDir.Properties["AppIsolated"][0] = "1";
newVDir.Properties["AppRoot"][0] =
"/LM" +
metabasePath.Substring(metabasePath.IndexOf("/", ("IIS://".Length)));
newVDir.CommitChanges();
Console.WriteLine(" Done.");
}
else
Console.WriteLine(
" Failed. A virtual directory can only be created in a site or virtual directory node.");
}