Results 1 to 40 of 65

Thread: Create Virtual Directory in IIS using VB.NET

Threaded View

  1. #1
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 01
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Create Virtual Directory in IIS using VB.NET

    You need to add a reference into your app for System.DirectoryServices
    Then use:
    VB Code:
    1. Private Sub CreateVirtualDir(ByVal WebSite As String, ByVal AppName As String, ByVal Path As String)
    2.  
    3.         Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/Schema/AppIsolated")
    4.         Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
    5.         IISSchema.Dispose()
    6.  
    7.         If CanCreate Then
    8.             Dim PathCreated As Boolean
    9.  
    10.             Try
    11.                 Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")
    12.  
    13.                 'make sure folder exists
    14.                 If Not System.IO.Directory.Exists(Path) Then
    15.                     System.IO.Directory.CreateDirectory(Path)
    16.                     PathCreated = True
    17.                 End If
    18.  
    19.                 'If the virtual directory already exists then delete it
    20.                 For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
    21.                     If VD.Name = AppName Then
    22.                         IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
    23.                         IISAdmin.CommitChanges()
    24.                         Exit For
    25.                     End If
    26.                 Next VD
    27.  
    28.                 'Create and setup new virtual directory
    29.                 Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir")
    30.                 VDir.Properties("Path").Item(0) = Path
    31.                 VDir.Properties("AppFriendlyName").Item(0) = AppName
    32.                 VDir.Properties("EnableDirBrowsing").Item(0) = False
    33.                 VDir.Properties("AccessRead").Item(0) = True
    34.                 VDir.Properties("AccessExecute").Item(0) = True
    35.                 VDir.Properties("AccessWrite").Item(0) = False
    36.                 VDir.Properties("AccessScript").Item(0) = True
    37.                 VDir.Properties("AuthNTLM").Item(0) = True
    38.                 VDir.Properties("EnableDefaultDoc").Item(0) = True
    39.                 VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp"
    40.                 VDir.Properties("AspEnableParentPaths").Item(0) = True
    41.                 VDir.CommitChanges()
    42.  
    43.                 'the following are acceptable params
    44.                 'INPROC = 0
    45.                 'OUTPROC = 1
    46.                 'POOLED = 2
    47.                 VDir.Invoke("AppCreate", 1)
    48.  
    49.             Catch Ex As Exception
    50.                 If PathCreated Then
    51.                     System.IO.Directory.Delete(Path)
    52.                 End If
    53.                 Throw Ex
    54.             End Try
    55.         End If
    56.     End Sub
    This is used in the following way:
    VB Code:
    1. CreateVirtualDir("LocalHost", "Woof", "C:\MyWebProjects\Woof")
    This creates a Virtual Dir called Woof on the LocalHost server and points and the path C:\MyWebProjects\Woof on the hard drive.

    Hope this helps.

    Woka

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •