:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  

Creating Virtual Directories in IIS using .NET

You can create virtual directories in IIS using .NET. Sample Vb.net code is pasted below please contact me for any further information to use it.

0001 Imports System
0002 Imports System.DirectoryServices
0003 
0004 
0005 Public Class IISManager
0006     Public Sub IISManager()
0007     End Sub
0008 
0009 
0010     Public Function CreateVDir(ByVal NewsiteName As StringByVal Path As StringByVal chkRead As BooleanByVal chkWrite As BooleanByVal chkExecute As BooleanByVal chkScript As BooleanByVal chkAuth As BooleanByVal serverName As StringByVal DefaultDoc As StringByVal dirList() As StringAs Boolean
0011 
0012         Dim sRet As String = String.Empty
0013         Dim IISSchema As System.DirectoryServices.DirectoryEntry
0014         Dim IISAdmin As System.DirectoryServices.DirectoryEntry
0015         Dim VDir As System.DirectoryServices.DirectoryEntry
0016         Dim IISUnderNT As Boolean
0017 
0018 
0019         ' Determine version of IIS
0020         IISSchema = New System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated")
0021         If (IISSchema.Properties("Syntax").Value.ToString().ToUpper() = "BOOLEAN"Then
0022             IISUnderNT = True
0023         Else
0024             IISUnderNT = False
0025         End If
0026 
0027 
0028         IISSchema.Dispose()
0029 
0030 
0031         'Get a Free SiteID
0032         Dim root As DirectoryEntry = New DirectoryEntry("IIS://" & serverName & "/W3SVC")
0033 
0034 
0035         Dim siteID As Integer = 1
0036 
0037 
0038         For Each e As DirectoryEntry In root.Children
0039             If e.SchemaClassName = "IIsWebServer" Then
0040                 Dim ID As Integer = Convert.ToInt32(e.Name)
0041                 If (ID >= siteID) Then
0042                     siteID = ID + 1
0043                 End If
0044             End If
0045         Next
0046 
0047 
0048         ''''''''''''''''Dim site = root.Invoke("Create","IIsWebServer", siteID)
0049         ''''''''''''''''site.Invoke("Put", "ServerComment",NewsiteName)
0050 
0051 
0052         ''''''''''''''''site.Invoke("Put", "KeyType", "IIsWebServer")
0053         ''''''''''''''''site.Invoke("Put", "ServerBindings", ":80:")
0054         ''''''''''''''''site.Invoke("Put", "ServerState", 2)
0055         ''''''''''''''''site.Invoke("Put", "FrontPageWeb", 1)
0056         ''''''''''''''''site.Invoke("Put", "DefaultDoc", DefaultDoc)
0057         '''''''''''''''''site.Invoke("Put", "SecureBindings", ":443:")
0058         ''''''''''''''''site.Invoke("Put", "ServerAutoStart", 1)
0059         ''''''''''''''''site.Invoke("Put", "ServerSize", 1)
0060         ''''''''''''''''site.Invoke("SetInfo")
0061 
0062 
0063         ''''''''''''''''site.CommitChanges()
0064 
0065 
0066         siteID = 312010922
0067 
0068 
0069         '
0070         ' Get the admin object
0071         '
0072         IISAdmin = New System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/W3SVC/" + siteID.ToString() + "/Root")
0073 
0074 
0075         '
0076         ' If the virtual directory already exists then delete it
0077         '
0078         For Each v As System.DirectoryServices.DirectoryEntry In IISAdmin.Children()
0079             ' Delete the specified virtual directory if it already
0080             exists()
0081             Try
0082                 IISAdmin.Invoke("Delete"New String() {v.SchemaClassName, v.Name})
0083                 IISAdmin.CommitChanges()
0084             Catch ex As Exception
0085             End Try
0086         Next
0087 
0088 
0089         '
0090         ' Create the virtual directory
0091         '
0092         Dim ii As Integer = 0
0093 
0094 
0095         For ii = 0 To dirList.Length - 1
0096             Dim dirName = dirList(ii).Split("")(dirList(ii).Split("").Length - 1).ToUpper()
0097             If Not (dirName.toupper() = "BIN" Or dirName.toupper() = "BLL"Then
0098                 Try
0099                     VDir = IISAdmin.Children.Add(dirName, "IIsWebVirtualDir")
0100                     '
0101                     ' Setup the VDir
0102                     '
0103                     VDir.Properties("AccessRead")(0) = chkRead
0104                     VDir.Properties("AccessExecute")(0) = chkExecute
0105                     VDir.Properties("AccessWrite")(0) = chkWrite
0106                     VDir.Properties("AccessScript")(0) = chkScript
0107                     VDir.Properties("AuthNTLM")(0) = chkAuth
0108                     VDir.Properties("EnableDefaultDoc")(0) = True
0109                     VDir.Properties("EnableDirBrowsing")(0) = False
0110                     VDir.Properties("DefaultDoc")(0) = True
0111                     VDir.Properties("Path")(0) = Path
0112 
0113 
0114                     ' NT doesn't support this property
0115                     '
0116                     If (IISUnderNT = FalseThen
0117                         VDir.Properties("AspEnableParentPaths")(0) = True
0118                     End If
0119 
0120 
0121                     '
0122                     ' Set the changes
0123                     '
0124                     VDir.CommitChanges()
0125                     VDir.Invoke("Put""DefaultDoc", DefaultDoc)
0126                     VDir.Invoke("SetInfo")
0127                     '
0128                     ' Make it a web application
0129                     '
0130                     If (IISUnderNT = TrueThen
0131                         VDir.Invoke("AppCreate"False)
0132                     Else
0133                         VDir.Invoke("AppCreate", 1)
0134                     End If
0135                 Catch ex As Exception
0136 
0137 
0138                 End Try
0139             End If
0140         Next
0141 
0142 
0143         Return True
0144     End Function
0145 
0146 
0147     Public Property ServerName() As String
0148         Get
0149             Return _serverName
0150         End Get
0151         Set(ByVal Value As String)
0152             _serverName = Value
0153         End Set
0154     End Property
0155 
0156 
0157     Public Shared VirDirSchemaName As String = "IIsWebVirtualDir"
0158     Private _serverName As String
0159 
0160 
0161 End Class
 


Copyright 2005-2015, Megasolutions Ltd  |   Privacy Policy