Create a virtual Directory ASP.NET

Posted on 2006-03-22
Last Modified: 2013-11-15
I am using the following code in order to create a Virtual Directory in ASP.NET:

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.EventArgs" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%@ Page Language="VB" Debug="true" %>

<script language="VB" runat="server">
 Private Sub CreateVirtualDir(ByVal WebSite As String, ByVal AppName As String, ByVal Path As String)

                  path = "D:\Software"
                  AppName = "TestDir"
                  Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & "localhost" & "/Schema/AppIsolated")
            Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"

            If CanCreate Then
                Dim PathCreated As Boolean

                    Dim IISAdmin As New DirectoryEntry("IIS://LM/W3SVC/1529167522/Root")

                    'make sure folder exists
                    If Not System.IO.Directory.Exists(Path) Then
                        PathCreated = True
                    End If

                    'If the virtual directory already exists then delete it
                    For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
                        If VD.Name = AppName Then
                            IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
                            Exit For
                        End If
                    Next VD

                    'Create and setup new virtual directory
                    Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir")
                    VDir.Properties("Path").Item(0) = Path
                    VDir.Properties("AppFriendlyName").Item(0) = AppName
                    VDir.Properties("EnableDirBrowsing").Item(0) = False
                    VDir.Properties("AccessRead").Item(0) = True
                    VDir.Properties("AccessExecute").Item(0) = True
                    VDir.Properties("AccessWrite").Item(0) = False
                    VDir.Properties("AccessScript").Item(0) = True
                    VDir.Properties("AuthNTLM").Item(0) = True
                    VDir.Properties("EnableDefaultDoc").Item(0) = True
                    VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp"
                    VDir.Properties("AspEnableParentPaths").Item(0) = True

                    'the following are acceptable params
                    'INPROC = 0
                    'OUTPROC = 1
                    'POOLED = 2
                    VDir.Invoke("AppCreate", 1)

                Catch Ex As Exception
                    If PathCreated Then
                    End If
                    Throw Ex
                End Try
            End If
        End Sub


The problem is that I get the following error:

"BC30002: Type 'DirectoryEntry' is not defined."

This is the error line:                       Line 14:                   Dim IISSchema As New DirectoryEntry("IIS://" & "localhost" & "/Schema/AppIsolated")

Can you help me to figure out what is not working ?
Question by:performadsign
    LVL 39

    Expert Comment

    did you add reference System.DirectoryServices to your project or not?
    if not Add reference to System.DirectoryServices( Select project right cilck add reference, in the add reference dialog select System.DirectoryServices  and click add)
    LVL 6

    Accepted Solution

    LVL 10

    Expert Comment

    yes you can Create FTP and Web IIS Virtual Directory using C#

    see the following Link


    Author Comment

    Ok I did what "cnagaraj25"  suggested and it worked. I do not get any errors anymore. When I go to IIS Manager I cannot see my created virtual folder under the "Default Web Site". Also i changed the virtual directory entry to ("IIS://W3SVC/localhost/1/Root")

    Is there any way for me to check or make sure it was created? and would it be seen in the IIS manager

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
    The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
    This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
    This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now