Create IIS Virtual Directory

Applies To

OS:
VB:
NT, 9x, 2000
5, 6

Download... the project. (6 kb)

This is a question that is often asked in the newsgroups: How do you create a IIS virtual directory programmatically. Usually, whacky solutions are offered such as simply creating a folder under \inetpub\wwwroot. Well, solutions like this generally do not work because IIS folder is a bit more than just a folder. There are permissions involved, etc... And what if you want your IIS virtual folder to point to a directory other the one under \inetpub\wwwroot.

Included in the download you'll find a class that will allow you to create a virtual IIS folder the right way. The class includes the following properties & methods:

Method/Property Description
Create This method creates the virtual directory, dah! Returns True if successful. If not, get the error description from LastError property
VirtualDirectoryName The name of the virtual directory, you know, the stuff after http://localhost/...
PhysicalDirectoryName The folder to which the virtual directory points. This is where all your asp files are. If this folder does not exist on the disk, the class will create it for you.
ApplicationOwner Name of the user that runs the app. Default is IUSR_MACHINENAME
AllowScriptsToRun Indicates whether IIS will allow running ASP scripts.
RunApplicationInProcess Indicates whether your web app runs in IIS process.
LastError if Create method fails, this property contains the error

Below is an example of how easy it is to create a standard virtual directory.

    Dim oIIS As IISVirtualDirectory
    
    Set oIIS = New IISVirtualDirectory
    With oIIS
        .PhysicalDirectoryName = "c:\MyPhysicalDir"
        .VirtualDirectoryName = "MyVirtualDirectory"
        If .Create() Then
            MsgBox "Created"
        Else
            MsgBox .LastError
        End If
    End With
    
    Set oIIS = Nothing

That's all folks. Some notes: for setting permissions the class relies on the CACLS.EXE file that has shipped with NT, 2000, XP since 1993. If you are running WinNT or Win2000 on FAT or FAT32, the permissions are a moot point, since those file allocation schemes do not support permissions. Note that this article is based on code that Microsoft has provided for the ASP developers.

Download... the project. (6 kb)