Now that we understand the IIS metabase path, we can get our hands
dirty by writing some code that will actually create the virtual
directory.
First we need to put a reference to the System.DirectoryServices
namespace to access the DirectoryEntry object. Then we define a class (InetMgr)which
encapsulates the functionality that is needed. The relavent portions of
the code will be disccussed in this article. For full working sample,
you can download the zip file from the artifact section of this article.
We need to connect to the IIS server instance, so we declare an
object of DirectoryEntry type. We can get a reference to the web server
instance by passing in the metabase path of the server to this
DirectoryEntry object.
public void
Connect()
{
try
{
mIISServer =new DirectoryEntry("IIS://" + mServerName + "/W3SVC/1")
;
DirectoryEntry
rootFolder =mIISServer.Children.Find("Root", VirDirSchemaName);
mIISRootPath=rootFolder.Path;
}
catch (Exception
ex)
{
throw new Exception("Error while trying to connect to IIS server" +
mIISServer,ex);
}
}
|
Now that we
have a reference to the IIS server object we can now start creating the
virtual directory. We first start by getting a reference to the root
node of the web server by using the Children.Find method on the IIS
server DirectoryEntry object.
To create a virtual directory we need to add to this root node a new
node, which will be our virtual directory. Remember that all ADs are a
hierarchical representation, so we can traverse it like a tree.
We use the Children.Add method to add the virtual directory.
public void
CreateVirtualDirectory(string alias, string
directoryPath)
{
//Get a reference
to the root of the IIS
Server
DirectoryEntry rootFolder =mIISServer.Children.Find("Root", VirDirSchemaName);
try
{
rootFolder.RefreshCache();//Refresh the cache to commit any
pending changes
string virDirPath =mIISRootPath + "/" + alias +
"/";
//Create a Virtual
Directory
DirectoryEntry newVirDir
=rootFolder.Children.Add(alias,VirDirSchemaName);
//Set the properties for this
node
newVirDir.Properties["AccessRead"][0]=true
;
newVirDir.Properties["AccessScript"][0]=true;
newVirDir.Properties["Path"][0]=directoryPath
;
//Create an
Application
newVirDir.Invoke("AppCreate2",2);
newVirDir.CommitChanges()
;
rootFolder.CommitChanges()
;
newVirDir.Close()
;
rootFolder.Close()
;
mIISServer.CommitChanges()
;
mIISServer.Close()
;
}
catch (Exception
ex)
{
throw new Exception("Error Creating Virtual Directory "+ alias
+
Environment.NewLine + ex.Message,ex)
;
}
}
|
Next we need to set the various properties for this node. The
properties in the above code represent the settings for running an
ASP.NET web site. We also need to make the virtual directory function
as an application. The hitch is the IIS objects do not enable us to
call their underlying methods directly. The ADSI DirectoryEntry
representation of IIS doesn't enable us to use their full
functionality. However we can get to those methods through the Invoke
method of the DirectoryEntry object. But we need to understand the IIS
object functions and order and type of parameters. For a full list of
functions and parameters see the References section of this article.
For creating an application we use the AppCreate2 function with a parameter of 2 which indicates that it should be run in a pooled process.
Finally Commit all changes to the nodes and the server and close all
connections. The deletion of virtual directories is similar. (See the
implementation in the code sample download).