Showing posts with label Create Virtual Directory in IIS. Show all posts
Showing posts with label Create Virtual Directory in IIS. Show all posts

Tuesday, March 4, 2008

Creating Virtual Directories in IIS through coding

private void CreateVirtualDir(string WebSite, string AppName, string Path)
{
System.DirectoryServices.DirectoryEntry IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + WebSite + "/Schema/AppIsolated");

bool CanCreate = !(IISSchema.Properties("Syntax").Value.ToString.ToUpper() == "BOOLEAN");

IISSchema.Dispose();


if (CanCreate) {

bool PathCreated;

try {

System.DirectoryServices.DirectoryEntry IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + WebSite + "/W3SVC/1/Root");

//make sure folder exists

if (!System.IO.Directory.Exists(Path)) {
System.IO.Directory.CreateDirectory(Path);

PathCreated = true;

}

//If the virtual directory already exists then delete it

foreach (System.DirectoryServices.DirectoryEntry VD in IISAdmin.Children) {

if (VD.Name == AppName) {

IISAdmin.Invoke("Delete", new string[] {VD.SchemaClassName, AppName});

IISAdmin.CommitChanges();

break; // TODO: might not be correct. Was : Exit For

}

}

//Create and setup new virtual directory

System.DirectoryServices.DirectoryEntry VDir = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir");
VDir.Properties("Path").Item(0) = Path;

VDir.Properties("AppFriendlyName").Item(0) = AppName;

VDir.Properties("EnableDirBrowsing").Item(0) = false;

VDir.Properties("AccessRead").Item(0) = true;

VDir.Properties("AccessExecute").Item(0) = true;

VDir.Properties("AccessWrite").Item(0) = false;

VDir.Properties("AccessScript").Item(0) = true;

VDir.Properties("AuthNTLM").Item(0) = true;

VDir.Properties("EnableDefaultDoc").Item(0) = true;

VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp";

VDir.Properties("AspEnableParentPaths").Item(0) = true;

VDir.CommitChanges();


//the following are acceptable params

//INPROC = 0

//OUTPROC = 1

//POOLED = 2

VDir.Invoke("AppCreate", 1);
}


catch (Exception Ex) {

if (PathCreated) {

System.IO.Directory.Delete(Path);

}

throw Ex;

}

}

}