Create a Virtual Directory

Leave the Internet Services Manager behind and create vdirs programmatically instead.

Troubleshooting Tricks




Create a Virtual Directory

Leave the Internet Services Manager behind and create vdirs programmatically instead.


By Don Kiely


One of the common deployment tasks with ASP.NET applications is creating the virtual directory through which IIS passes page requests. Unlike ASP.old, where you might have to dump a bunch of information into the IIS metabase, creating a vdir is just about all you need to do beyond xcopy deployment. But rather than having to fire up Internet Services Manager and do it manually, wouldn't it be great to create vdir programmatically?


Of course it would - and this code (available for download) does exactly that. It creates an "aspnetPRO" virtual directory ready to run an ASP.NET application. It doesn't matter whether the path specified in the sPath variable exists or not; the code creates the vdir either way:


Dim sVDirName As String = "aspnetPRO"

Dim sPath As String = "C:\Inetpub\wwwroot\" & sVDirName

Dim sResult As String = "The " & sVDirName & " virtual directory was successfully created."



      Dim sMachine As String = System.Environment.MachineName

      Dim serverObject As Object

      serverObject = GetObject("IIS://" & sMachine & "/w3svc/1/Root")


      Dim vdirObject As Object

      vdirObject = serverObject.Create("IIsWebVirtualDir", sVDirName)



      vdirObject.AccessRead = True

      vdirObject.AccessScript = True

      vdirObject.AccessWrite = False

      vdirObject.AccessExecute = True

      vdirObject.EnableDirBrowsing = False


      vdirObject.Put("Path", (sPath))



      Dim directoryObject As Object

      directoryObject = GetObject("IIS://" & sMachine & "/W3SVC/1/ROOT/" & sVDirName)

      directoryObject.AppCreate3(True, "AppPool", 1)


Catch COMex As System.Runtime.InteropServices.COMException

      sResult = sResult.Replace("successfully", "<b><i>not</b></i>")

      sResult &= "<br><br><b>COM Interop exception:</b><br>Message: " & COMex.Message & "<br>Source:" & COMex.Source & "<br>Stack Trace:<br>" & COMex.StackTrace

Catch ex As Exception

      sResult = sResult.Replace("successfully", "<b><i>not</b></i>")

      sResult &= "<br><br><b>System exception:</b><br>Message: " & ex.Message & "<br>Source:" & ex.Source & "<br>Stack Trace:<br>" & ex.StackTrace



End Try


The code makes use of Windows Management Instrumentation (WMI) to get a reference to IIS and objects within it. The GetObject function calls pass WMI identifiers for an IIS server, creates the vdir, and calls the AppCreate3 method to create a Web application definition and mark it as running in-process, out-of-process, or in a process pool. There are plenty of other ways you can interact with IIS via WMI.


You can use this code in a variety of ways. The sample application that you can download contains a simple ASP.NET page that hard-codes the creation of an "aspnetPRO" virtual directory name and sets various properties. You also could create an administrative app accessible over the Web - with appropriate security, of course - and let admins create or change virtual directories.


My favorite use, however, is to build this code into an installer module so that deploying an app automatically creates the virtual directory for you. The latter requires using Microsoft Installer (MSI) packages, but VS .NET makes this simple. You can find an example of this use in the MSDN article, Modifying Internet Information Services During Deployment with Custom Actions , which has short but complex code to make the whole thing automatic and flexible.


The sample code in this article is available for download.


Don Kiely is senior technology consultant for Information Insights, a business and technology consultancy in Fairbanks, Alaska. E-mail him at mailto:[email protected].





Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.