Creating Virtual Web Sites on IIS with .Net
Creating Virtual Web Sites on IIS with .Net
My last post on Reading Web Sites Status on IIS with .Net was the first post on IIS development through .NET
So here is an example for creating virtual Web Site using .NET.
public DirectoryEntry VDir = null;
public string Virtual = "";
public string FriendlyName = "";
public string Path = "";
public string IISPath = "IIS://LOCALHOST/W3SVC/1/ROOT";
public bool AuthNTLM = true;
public bool AuthAnonymous = true;
public bool AuthBasic = true;
public string DefaultDocuments = default.htm,default.aspx,default.asp";
public bool CreateVirtual()
{
DirectoryEntry root = new DirectoryEntry(this.IISPath);
if (root == null)
{
//Couldn't access root node
return false;
}
try
{
this.VDir = root.Children.Add(Virtual, "IISWebVirtualDir");
}
catch
{
try { this.VDir = new DirectoryEntry(this.IISPath + "/" + Virtual); }
catch { ;}
}
if (this.VDir == null)
{
//Couldn't create virtual.
return false;
}
root.CommitChanges();
VDir.CommitChanges();
return this.SaveVirtualDirectory();
}
public bool SaveVirtualDirectory()
{
PropertyCollection Properties = VDir.Properties;
try
{
Properties["Path"].Value = Path;
}
catch (Exception ex)
{
//Invalid Path provided
return false;
}
this.VDir.Invoke("AppCreate", true);
if (this.FriendlyName == "")
VDir.Properties["AppFriendlyName"].Value = Virtual;
else
VDir.Properties["AppFriendlyName"].Value = this.FriendlyName;
if (this.DefaultDocuments != "")
VDir.Properties["DefaultDoc"].Value = this.DefaultDocuments;
int Flags = 0;
if (this.AuthAnonymous)
Flags = 1;
if (this.AuthBasic)
Flags = Flags + 2;
if (this.AuthNTLM)
Flags = Flags + 4;
Properties["AuthFlags"].Value = Flags;
VDir.CommitChanges();
return true;
}