My Projects
Search Blog

Categories
Archives
Useful Links
Photo Albums
RSS

Powered by
BlogCFM v1.15

Vivio Technologies CFML Hosting
15 June 2008
Creating IIS Virtual Directories with Coldfusion 8 and .NET integr

Ever had a need to automate the creation of virtual directories in IIS?  Coldfusion 8 can do this quite well using the System.DirectoryServices.DirectoryEntry class in the .NET framework.

After a whole lot of trial and error, this is what I came up with and it seems to work quite nicely:

 

<!--- get the server ID for the specific web site
    from IIS.  The default web site is usually "1"
    --->
<cfset serverid="2017647531">

    <!---
        //  metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
        //    for example "IIS://localhost/W3SVC/1/Root"
    --->
<cfset metabasePath = "IIS://localhost/W3SVC/#serverid#/Root">

<!--- name of the virtual directory --->
<cfset vdirName = "vdir001">

<!--- physical path for this virtual directory --->
<cfset physicalPath = "D:\Inetpub\ricktest\realdir">

<!--- full path to the System.DirectoryServices.dll --->
<cfset dllPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.DirectoryServices.dll">

<!--- instantiate a DirectoryEntry .NET object --->
<cfobject
    type=".net"
    class="System.DirectoryServices.DirectoryEntry"
    assembly="#dllPath#"
    name="site">

<!--- initialize the object with the metabase path --->   
<cfset site.init(metabasePath)>

<!--- you need the classname for later --->
<cfset className = site.Get_SchemaClassName()>

<cfif reFind("VirtualDir$",className) gt 0>
    <!--- Get the children so we can add a new child --->
    <cfset vdirs = site.Get_Children()>
    <cftry>
        <!--- add the new virtual directory --->
        <cfset newVDir = vdirs.Add(vDirName, className)>
        <cfcatch type="any">
            <cfoutput><h3>Failed to create #vDirName#</h3></cfoutput>
            <cfif cfcatch.ToString() contains "already exists">
                <p>Another directory with that name already exists.</p>
            <cfelse>
                <cfoutput><p>#cfcatch.ToString()#</p></cfoutput>
            </cfif>
            <cfabort>
        </cfcatch>
    </cftry>
    <Cftry>
        <!--- set the PAth property to the physical directory path --->
        <cfset newVDir.Get_Properties().Get_Item("Path").Set_Value(physicalPath)>
        <!--- commit the changes --->
        <cfset newVDir.CommitChanges()>

        <cfcatch type="any">
            <cfdump var="#cfcatch.ToString()#">
            <cfdump var="#cfcatch#">
            <Cfabort>
        </cfcatch>
    </cftry>
</cfif>


 

 

Posted by rickroot at 9:08 AM | Link | 4 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Creating IIS Virtual Directories with Coldfusion 8 and .NET integr
Rick, I also accomplished the same as a CFC using the CFExecute tag and the iisvdir.vbs script that comes with IIS6.0+ -- which is nice because it works in CF6 and up: http://iisvdir.riaforge.org/
Posted by adam.tuttle on June 23, 2008 at 11:29 AM

Re: Creating IIS Virtual Directories with Coldfusion 8 and .NET integr
Is this for IIS7? If not, can it be altered to work with it? Also, how would one make this also create the bindings and app pools for IIS7?
Posted by webopolis on September 12, 2008 at 10:16 PM

Re: Creating IIS Virtual Directories with Coldfusion 8 and .NET integr
This line is failing for me:

The error I'm getting is:
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation." The lengthy message includes "The system cannot find the path specified."
For testing, suppose I want to duplicate my site's images folder to www.domain.com/imagesDup/.
My site root is located at "D:\siteName\files\" so I set "physicalPath" to "D:\siteName\files\images".
I set vdirName to "imagesDup".
What am I missing?
Thanks.
Posted by richard on November 5, 2008 at 4:43 PM

Re: Creating IIS Virtual Directories with Coldfusion 8 and .NET integr
Try again, here's the line failing:
cfset newVDir.Get_Properties().Get_Item("Path").Set_Value(physicalPath)
Posted by richard on November 5, 2008 at 4:44 PM

Post a comment (login required)