DirectoryServices.DirectoryEntry - Creating virtual directories in

G

Guest

I'm using the DirectoryServices class to try and create new virtual
directories in IIS.
Here's the code I'm using:

System.DirectoryServices.DirectoryEntry oDE;
System.DirectoryServices.DirectoryEntries oDC;
System.DirectoryServices.DirectoryEntry oVirDir;
string sIISPath = @"IIS:\\localhost\W3SVC\1\Root";
//Assuming it exists, we bind our DirectoryEntry object, oDE, to this root
using:
if (VirDirExists(sIISPath))
{
oDE = new System.DirectoryServices.DirectoryEntry(sIISPath);
//We obtain a collection of all of the children virtual directories under
this root object using:
oDC = oDE.Children;
//Add a new member to this collection and bind it to a new DirectoryEntry
object, oVirDir, thus creating a new
//virtual directory with:
oVirDir = oDC.Add(virDirName, oDE.SchemaClassName.ToString());
//Commit the changes to the IIS Metabase.
oVirDir.CommitChanges();
//Setting and Updating Any of the Properties on IIS
//We additionally need to assign a physical path to this new virtual
directory, using a straightforward
//property-setting approach:
oVirDir.Properties["Path"][0]= physicalPath;
oVirDir.Properties["DefaultDoc"][0] = "home_here.aspx";
oVirDir.Properties["AppFriendlyName"][0] = textBox1.Text.Trim();
oVirDir.Properties["CreateProcessAsUser"][0] = true;
oVirDir.CommitChanges();

This works fine in that it adds the entry to the default web site and set's
the appropriate properties. However, it does not 'create' the application.
You need to physically go into the virtual directory properties(in the IIS
snapin), go to the Virtual Directory tab, and click the <CREATE> button next
to the Application Name textbox in order to get the virtual directory to work
in IIS.

I can't seem to find an method that does this programatically in the
DirectoryServices class. Does any one knows what this would be or if there
is another way to create virtual directories programatically in IIS?

Thanks for your help

Paul
 
G

Guest

Hey Paul,

You need to set the AppRoot property as well. This will cause the
application to be created. E.g.:

vdir.Properties["AppRoot"].Value = "/LM/W3SVC/1/Root/TESTSITE";

HTH, Jakob.
 
G

Guest

That was it.
Thanks!

Jakob Christensen said:
Hey Paul,

You need to set the AppRoot property as well. This will cause the
application to be created. E.g.:

vdir.Properties["AppRoot"].Value = "/LM/W3SVC/1/Root/TESTSITE";

HTH, Jakob.
--
http://www.dotninjas.dk
http://www.powerbytes.dk


paul said:
I'm using the DirectoryServices class to try and create new virtual
directories in IIS.
Here's the code I'm using:

System.DirectoryServices.DirectoryEntry oDE;
System.DirectoryServices.DirectoryEntries oDC;
System.DirectoryServices.DirectoryEntry oVirDir;
string sIISPath = @"IIS:\\localhost\W3SVC\1\Root";
//Assuming it exists, we bind our DirectoryEntry object, oDE, to this root
using:
if (VirDirExists(sIISPath))
{
oDE = new System.DirectoryServices.DirectoryEntry(sIISPath);
//We obtain a collection of all of the children virtual directories under
this root object using:
oDC = oDE.Children;
//Add a new member to this collection and bind it to a new DirectoryEntry
object, oVirDir, thus creating a new
//virtual directory with:
oVirDir = oDC.Add(virDirName, oDE.SchemaClassName.ToString());
//Commit the changes to the IIS Metabase.
oVirDir.CommitChanges();
//Setting and Updating Any of the Properties on IIS
//We additionally need to assign a physical path to this new virtual
directory, using a straightforward
//property-setting approach:
oVirDir.Properties["Path"][0]= physicalPath;
oVirDir.Properties["DefaultDoc"][0] = "home_here.aspx";
oVirDir.Properties["AppFriendlyName"][0] = textBox1.Text.Trim();
oVirDir.Properties["CreateProcessAsUser"][0] = true;
oVirDir.CommitChanges();

This works fine in that it adds the entry to the default web site and set's
the appropriate properties. However, it does not 'create' the application.
You need to physically go into the virtual directory properties(in the IIS
snapin), go to the Virtual Directory tab, and click the <CREATE> button next
to the Application Name textbox in order to get the virtual directory to work
in IIS.

I can't seem to find an method that does this programatically in the
DirectoryServices class. Does any one knows what this would be or if there
is another way to create virtual directories programatically in IIS?

Thanks for your help

Paul
 
G

Guest

I can't seem to find an method that does this programatically in the
DirectoryServices class. Does any one knows what this would be or if
there
is another way to create virtual directories programatically in IIS?

Paul,

You should invoke the method AppCreate, AppCreate2 or AppCreate3 on the
IIsWebDirectory directory entry object, eg:

public void CreateApplication(string path, string name, string
applicationPool, IIsInProcessFlags flag)
{
if (path == null)
throw new ArgumentNullExpcetion("path");

if (name == null)
throw new ArgumentNullException("name");

using (DirectoryEntry entry = new DirectoryEntry(path))
{
entry.Invoke("AppCreate2", new object[1] { (int)flag });
entry.Properties["AppFriendlyName"].Value = name;
if (applicationPool != null)
entry.Properties["AppPoolId"].Value = applicationPool;
entry.CommitChanges();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top