• 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"
            IISSchema.Dispose()

            If CanCreate Then
                Dim PathCreated As Boolean

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

                    'make sure folder exists
                    If Not System.IO.Directory.Exists(Path) Then
                        System.IO.Directory.CreateDirectory(Path)
                        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})
                            IISAdmin.CommitChanges()
                            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
                    VDir.CommitChanges()

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

                Catch Ex As Exception
                    If PathCreated Then
                        'System.IO.Directory.Delete(Path)
                    End If
                    Throw Ex
                End Try
            End If
        End Sub

</script>

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 ?
0
performadsign
Asked:
performadsign
1 Solution
 
appariCommented:
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)
0
 
cnagaraj25Commented:
0
 
SystemExpertCommented:
yes you can Create FTP and Web IIS Virtual Directory using C#

see the following Link

http://www.c-sharpcorner.com/Code/2002/July/CreateVirtualDirs.asp

Thanks
0
 
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
 
0

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