public class VirtualDirectory {
public WebServerType ServerType = WebServerType.IIS6;
public string Virtual = "";
public string FriendlyName = "";
public string Path = "";
public int SiteId = -1;
public string IISPath = "IIS://LOCALHOST/W3SVC/1/ROOT";
public string ApplicationPool = ""; // n/a
public bool AuthNTLM = true;
public bool AuthAnonymous = true;
public bool AuthBasic = true;
public string DefaultDocuments = "default.htm,default.aspx,default.asp";
public VirtualDirectory(OSType osType) {
if (osType == OSType.XP) {
IISPath = "IIS://" + this.DomainName + "/W3SVC";
}
else {
IISPath = "IIS://" + System.Environment.MachineName + "/W3SVC";
}
}
///
/// Used for GetWebSites to retrieve sites for a given domain or IP
///
public string DomainName = "localhost";
///
/// Contains Virtual directory entry (as a DirectoryEntry object) after
/// the virtual was created.
///
public DirectoryEntry VDir = null;
///
/// Contains error message on a failure result.
///
public string ErrorMessage = "";
///
/// Returns a list of Web Sites on the local machine
///
///
public WebSiteEntry[] GetWebSites( ) {
DirectoryEntry root = null;
try {
root = new DirectoryEntry(IISPath);
}
catch {
this.SetError("Couldn't access root node");
return null;
}
if (root == null) {
this.SetError("Couldn't access root node");
return null;
}
ArrayList al = new ArrayList(20);
foreach (DirectoryEntry Entry in root.Children) {
PropertyCollection Properties = Entry.Properties;
try {
WebSiteEntry Site = new WebSiteEntry();
Site.SiteName = (string)Properties["ServerComment"].Value;
Site.IISPath = Entry.Path;
if (Site.SiteName != null)
al.Add(Site);
}
catch { ; }
}
root.Close();
return (WebSiteEntry[])al.ToArray(typeof(WebSiteEntry));
}
public bool CreateVirtual(bool includeRoot)
{
this.SetError(null);
string virtualPath;
if (includeRoot )
virtualPath = this.IISPath + "/ROOT";
else
virtualPath = this.IISPath;
DirectoryEntry root = new DirectoryEntry(virtualPath);
if (root == null)
{
this.SetError("Couldn't access root node");
return false;
}
try
{
this.VDir = root.Children.Add(Virtual, "IISWebVirtualDir");
}
catch
{
try { this.VDir = new DirectoryEntry(virtualPath + "/" + Virtual); }
catch { ;}
}
if (this.VDir == null)
{
this.SetError("Couldn't create virtual.");
return false;
}
root.CommitChanges();
VDir.CommitChanges();
return this.SaveVirtualDirectory();
}
///
/// Creates a Virtual Directory on the Web Server and sets a few
/// common properties based on teh property settings of this object.
///
///
public bool CreateWebSite( )
{
this.SetError(null);
DirectoryEntry root = new DirectoryEntry(this.IISPath);
if (root == null) {
this.SetError("Couldn't access root node");
return false;
}
try {
//int siteId = 1;
if (SiteId == -1)
{
foreach (DirectoryEntry ent in root.Children)
{
if (ent.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(ent.Name);
if (ID >= SiteId)
{
SiteId = ID + 1;
}
}
}
}
else
{
root.Invoke("Delete", "IISWebServer", SiteId);
root.CommitChanges();
}
this.VDir = (DirectoryEntry)root.Invoke("Create", "IISWebServer", SiteId);
this.VDir.Invoke("Put", "ServerComment", this.Virtual);
this.VDir.Invoke("Put", "KeyType", "IIsWebServer");
this.VDir.Invoke("Put", "ServerBindings", ":80:");
this.VDir.Invoke("Put", "ServerState", 2);
this.VDir.Invoke("Put", "FrontPageWeb", 1);
this.VDir.Invoke("Put", "DefaultDoc", "default.htm,default.asp,default.aspx");
this.VDir.Invoke("Put", "SecureBindings", ":443:");
this.VDir.Invoke("Put", "ServerAutoStart", 1);
this.VDir.Invoke("Put", "ServerSize", 1);
this.VDir.Invoke("SetInfo");
//' Create application virtual directory
DirectoryEntry siteVDir = this.VDir.Children.Add("Root", "IIsWebVirtualDir");
siteVDir.Properties["AppIsolated"].Value = 2;
siteVDir.Properties["Path"].Value = this.Path;
siteVDir.Properties["AccessFlags"].Value = 513;
siteVDir.Properties["FrontPageWeb"].Value = 1;
siteVDir.Properties["AppRoot"].Value = @"/LM/W3SVC/" + SiteId + "/Root";
siteVDir.Properties["AppFriendlyName"].Value = "Default Application";
siteVDir.CommitChanges();
this.IISPath = this.IISPath + "/" + SiteId;
}
catch( Exception e ) {
//try { this.VDir = new DirectoryEntry(this.IISPath + "/" + Virtual); }
//catch { ;}
}
if (this.VDir == null) {
this.SetError("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) {
this.SetError("Invalid Path provided " + ex.Message);
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; // NTLM AuthBasic Anonymous
VDir.CommitChanges();
return true;
}
protected void SetError(string ErrorMessage) {
if (ErrorMessage == null)
this.ErrorMessage = "";
this.ErrorMessage = ErrorMessage;
}
/// <summary>
/// wscript IISWildcardHandler.vbs W3SVC/2/Browse/ScriptMaps \"\" \"*,C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_isapi.dll,0\" /INSERT /COMMIT
/// </summary>
/// <param name="sharpForgeHome"></param>
/// <returns></returns>
public bool ConfigureWildcardHandler( string sharpForgeHome, bool includeRoot )
{
///LM/W3SVC/1/Root/SharpForge2/Browse
//configure wildcard handler
string commandLine = "";
if ( includeRoot )
commandLine = " IISWildcardHandler.vbs " + this.IISPath.Substring(this.IISPath.IndexOf("W3SVC")) + @"/ROOT/" + Virtual + @"/ScriptMaps """" ""*,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,0"" /INSERT /COMMIT";
else
commandLine = " IISWildcardHandler.vbs " + this.IISPath.Substring(this.IISPath.IndexOf("W3SVC")) + @"/ScriptMaps """" ""*,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,0"" /INSERT /COMMIT";
Process p = new Process();
//p.StartInfo.FileName = SharpForgeHome + "\\Installer\\IISWildcardHandler.vbs";
p.StartInfo.FileName = "wscript.exe";
p.StartInfo.WorkingDirectory = sharpForgeHome + "\\Installer";
p.StartInfo.Arguments = commandLine + " //B";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.StartInfo.CreateNoWindow = true;
p.Start();
return true;
}
// Register as ASP.NET 2.0 vdir
/// <summary>
/// C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_regiis.exe -sn W3SVC/2/asdfasdf
/// </summary>
/// <returns></returns>
public bool RegisterAsAspNet2( bool isVirtualDirectory ){
string frameworkVersion = System.Configuration.ConfigurationManager.AppSettings["frameworkVersion"];
string winPath = Environment.GetEnvironmentVariable("windir");
string fullPath = winPath + @"\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe ";
//string args = " -sn " + "W3SVC/" + webSiteNum + "/root/" + VDirName;
string args = "";
if (isVirtualDirectory)
{
args = " -sn " + this.IISPath.Substring(this.IISPath.IndexOf("W3SVC")) + "/ROOT/" + Virtual; //" -sn W3SVC/2/Browse"
}
else
{
args = " -sn " + this.IISPath.Substring(this.IISPath.IndexOf("W3SVC")); //" -sn W3SVC/2/Browse"
}
System.Diagnostics.Process _p = new System.Diagnostics.Process();
_p.StartInfo.CreateNoWindow = true;
_p.StartInfo.FileName = fullPath;
_p.StartInfo.Arguments = args;
_p.StartInfo.UseShellExecute = false;
_p.StartInfo.RedirectStandardOutput = true;
_p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
_p.Start();
//sRet += "VRoot " + VDirName + " created!";
return true;
}
}
public enum WebServerType {
IIS4, IIS5, IIS6
}
