• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

Create a virtual Directory ASP.NET

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 ?
1 Solution
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)
yes you can Create FTP and Web IIS Virtual Directory using C#

see the following Link


performadsignAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now