My ZoneX

Waiting for Blogger Beta2!!

Thursday, December 01, 2005

IIS 6 - Virtual Directories Management with C#

C# - Visual Studio 2003
Programmatically Creating Virtual Directory and Deleting the Existing Virtual Directory on IIS 6.0?

#region Create Virtual Directory
/*
* Usage : CreateVirtualDirectory("localhost","MyWebApplication");
*
*/
public static bool CreateVirtualDirectory(string sWebSite,string sAppName,string sPath)
{
System.DirectoryServices.DirectoryEntry iISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + sWebSite + "/Schema/AppIsolated");
bool bCanCreate = !( iISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN");
iISSchema.Dispose();

if(bCanCreate)
{
bool bPathCreated=false;
try
{
System.DirectoryServices.DirectoryEntry iISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + sWebSite + "/W3SVC/1/Root");

//make sure folder exists
if(! System.IO.Directory.Exists(sPath))
{
System.IO.Directory.CreateDirectory(sPath);
bPathCreated = true;
}

//If the virtual directory already exists then delete it
foreach(System.DirectoryServices.DirectoryEntry vd in iISAdmin.Children)
{
if(vd.Name==sAppName)
{
//iISAdmin.Invoke("Delete", new string(){vd.SchemaClassName,AppName};);
iISAdmin.Invoke("Delete", new string[]{vd.SchemaClassName, sAppName});
iISAdmin.CommitChanges();
break;
}
}


//Create and setup new virtual directory
System.DirectoryServices.DirectoryEntry vdir = iISAdmin.Children.Add(sAppName, "IIsWebVirtualDir");

vdir.Properties["Path"][0] = sPath;
vdir.Properties["AppFriendlyName"][0] = sAppName;
vdir.Properties["EnableDirBrowsing"][0] = false;
vdir.Properties["AccessRead"][0] = true;
vdir.Properties["AccessExecute"][0] = true;
vdir.Properties["AccessWrite"][0] = false;
vdir.Properties["AccessScript"][0] = true;
vdir.Properties["AuthNTLM"][0] = true;
vdir.Properties["EnableDefaultDoc"][0] = true;
vdir.Properties["DefaultDoc"][0] = "default.htm,default.aspx,default.asp";
vdir.Properties["AspEnableParentPaths"][0] = true;
vdir.CommitChanges();

//'the following are acceptable params
//'INPROC = 0
//'OUTPROC = 1
//'POOLED = 2
vdir.Invoke("AppCreate", 1);

return true;
}
catch(Exception ex)
{
if(bPathCreated)
{
System.IO.Directory.Delete(sPath);
throw ex;
}
}
}
return false;
}
#endregion



#region Delete Virtual Directory
/*
* Usage : DeleteVirtualDirectory("localhost","MyWebApplication");
*
*/
public static bool DeleteVirtualDirectory(string sWebSite,string sAppName)
{
System.DirectoryServices.DirectoryEntry iISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + sWebSite + "/Schema/AppIsolated");
bool bCanCreate = !( iISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN");
iISSchema.Dispose();

if(bCanCreate)
{
try
{
System.DirectoryServices.DirectoryEntry iISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + sWebSite + "/W3SVC/1/Root");

string sWebPath = iISAdmin.Properties["Path"].Value.ToString();

//If the virtual directory already exists then delete it
foreach(System.DirectoryServices.DirectoryEntry vd in iISAdmin.Children)
{
if(vd.Name==sAppName)
{
sWebPath += "\\" + vd.Name;

//if(((System.DirectoryServices.PropertyCollection)((vd.Properties))).valueTable.Count > 0 )
//Original = IIsWebDirectory
//Custom = IIsWebVirtualDir
if(vd.Properties["KeyType"].Value.ToString().Trim() == "IIsWebVirtualDir")
sWebPath=vd.Properties["Path"].Value.ToString();

//iISAdmin.Invoke("Delete", new string(){vd.SchemaClassName,AppName};);
iISAdmin.Invoke("Delete", new string[]{vd.SchemaClassName, sAppName});
System.IO.Directory.Delete(sWebPath);
iISAdmin.CommitChanges();
return true;
}
}
}
catch(Exception ex)
{
return false;
}
}
return false;
}
#endregion

Some other informations related to IIS & .NET 2003

Create a Virtual Directory and Edit its Properties in IIS using C#
http://dotnetjunkies.com/WebLog/ramdash/articles/21777.aspx

Create Virtual Directory in IIS using VB.NET
http://www.vbforums.com/printthread.php?t=347207

Create Virtual Directory in IIS using C# [Easy Way]
System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot();
string sErr=null;
vr.Create("IIS://localhost/W3SVC/1/Root",@"C:\Sync\","WebServices",out sErr);
Console.WriteLine(sErr);

Virtual Directory
http://www.c-sharpcorner.com/Code/2002/July/CreateVirtualDirs.asp

Creating your own Web Server using C#
http://www.c-sharpcorner.com/Internet/CreatingWebServerInCSIMA.asp

*******

To help a person who don't know about creating IIS. esp: beginner. :-)

[Using INetMgr]
===============

IIS Applications and Virtual Directories
http://authors.aspalliance.com/PaulWilson/Articles/?id=16

How to: Create and Configure Virtual Directories in IIS
http://msdn2.microsoft.com/en-us/library/zwk103ab.aspx

Internet Information Services (IIS) 6.0 Resource Kit
http://www.microsoft.com/downloads/details.aspx?FamilyID=80a1b6e6-829e-49b7-8c02-333d9c148e69&DisplayLang=en

How To Install MetaEdit 2.2 on Windows NT 4.0 or Windows 2000?
If you wanna know about IIS in deep, you should install this exe. This tool will let you know about IIS structures such as where IIS store all information about web folder. I would say that it's really interesting tool. Enjoy :-)
http://support.microsoft.com/default.aspx?scid=kb;en-us;301386&sd=tech
Hints :
What is MetaEdit 2.2?
MetaEdit 2.2 is designed to help administer Microsoft® Internet Information Server for Windows NT® Server 4.0, and Microsoft® Internet Information Services for Windows 2000®.

What is the Metabase?
The metabase is a hierarchical database that is used to store configuration values for IIS. In previous versions of IIS, such values were configurable by directly editing the registry. Some values are still configurable by editing the registry, but the metabase provides more granularity in the configuration of server properties. You can set server properties at the computer, Web site, virtual directory, directory, and file level by modifying the metabase.

1 Comments:

  • At 1:37 AM , Anonymous Anonymous said...

    Very nice Michael,keep up the good work! :)
    -Raul

     

Post a Comment

Subscribe to Post Comments [Atom]

<< Home