Results 1 to 6 of 6

Thread: Need to answers for questions on my Create IIS VD code.

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Need to answers for questions on my Create IIS VD code.

    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.                 'make sure folder exists
    12.                 If Not System.IO.Directory.Exists(Path) Then
    13.                     System.IO.Directory.CreateDirectory(Path)
    14.                     PathCreated = True
    15.                 End If
    16.  
    17.                 'create IIS object
    18.                 Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")
    19.  
    20.                 'If the virtual directory already exists then delete it
    21.                 For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
    22.                     If VD.Name = AppName Then
    23.                         IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
    24.                         IISAdmin.CommitChanges()
    25.                         Exit For
    26.                     End If
    27.                 Next VD
    28.  
    29.                 'Create and setup new virtual directory
    30.                 Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir")
    31.  
    32.                 VDir.Properties("Path").Item(0) = Path
    33.                 VDir.Properties("AppFriendlyName").Item(0) = AppName
    34.                 VDir.Properties("EnableDirBrowsing").Item(0) = False
    35.                 VDir.Properties("AccessRead").Item(0) = True
    36.                 VDir.Properties("AccessExecute").Item(0) = True
    37.                 VDir.Properties("AccessWrite").Item(0) = False
    38.                 VDir.Properties("AccessScript").Item(0) = True
    39.                 VDir.Properties("AuthNTLM").Item(0) = True
    40.                 VDir.Properties("EnableDefaultDoc").Item(0) = True
    41.                 VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp"
    42.                 VDir.Properties("AspEnableParentPaths").Item(0) = True
    43.                 VDir.CommitChanges()
    44.  
    45.                 'the following are acceptable params
    46.                 'INPROC = 0
    47.                 'OUTPROC = 1
    48.                 'POOLED = 2
    49.                 VDir.Invoke("AppCreate", 1)
    50.  
    51.             Catch Ex As Exception
    52.                 If PathCreated Then
    53.                     System.IO.Directory.Delete(Path)
    54.                 End If
    55.                 Throw Ex
    56.             End Try
    57.         End If
    This is used like:
    VB Code:
    1. CreateVirtualDir("LocalHost", "Woof", "C:\My Web Apps\Woof")
    I completely understand all of the code, except for one line:
    VB Code:
    1. Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
    Why? What's it checking? All the code I find on the web has this in, but can't figure out what exactly it's there for

    Woka

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Need to answers for questions on my Create IIS VD code.

    That determines whether it's IIS 5+ or not. Apparently this code works on IIS 5 and above.

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Need to answers for questions on my Create IIS VD code.

    Eugh! That's horrible. Is there a neater method? there must be. ie:
    VB Code:
    1. Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")
    2.    If IISAdmin.Version >=5 Then
    3.       'create VD
    4.    End If
    The code I have now is un readable and doesn't "make sense" unless you know what it does. I now know what it does as u've told me, but future developers here won't. Yea yea, I could add comments, but I'd rather have a much readable way.

    Woka

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Need to answers for questions on my Create IIS VD code.

    Yes that'd be nice, but I have no clue why it's this way. Btw, all code I've seen similar to yours uses the boolean "IsUnderNT" instead of "CanCreate"

  5. #5

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Need to answers for questions on my Create IIS VD code.

    Also, I have been neatening other bits of code up and found that:
    VB Code:
    1. If VD.Name = AppName Then
    2.    IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
    3.    IISAdmin.CommitChanges()
    4.    Exit For
    5. End If
    can be replaced by:
    VB Code:
    1. If VD.Name = AppName Then
    2.    VD.DeleteTree()
    3.    Exit For
    4. End If
    Which I think is much much neater.
    Any reason for not using the 2nd method?

    Woka

  6. #6

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Need to answers for questions on my Create IIS VD code.

    Quote Originally Posted by mendhak
    Yes that'd be nice, but I have no clue why it's this way. Btw, all code I've seen similar to yours uses the boolean "IsUnderNT" instead of "CanCreate"
    yea, but all the code out there similar to mine is shoddy. I always find code and then tear it apart. Admittedly I feel your comment is justified, and this can be solved with better naming on my behalf, but I still want a .Version property type method instead of that weird funny way.

    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
  •  



Click Here to Expand Forum to Full Width