Need to answers for questions on my Create IIS VD code.
VB Code:
Private Sub CreateVirtualDir(ByVal WebSite As String, ByVal AppName As String, ByVal Path As String)
Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/Schema/AppIsolated")
Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
IISSchema.Dispose()
If CanCreate Then
Dim PathCreated As Boolean
Try
'make sure folder exists
If Not System.IO.Directory.Exists(Path) Then
System.IO.Directory.CreateDirectory(Path)
PathCreated = True
End If
'create IIS object
Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")
'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
This is used like:
VB Code:
CreateVirtualDir("LocalHost", "Woof", "C:\My Web Apps\Woof")
I completely understand all of the code, except for one line:
VB Code:
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
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.
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:
Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")
If IISAdmin.Version >=5 Then
'create VD
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
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"
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:
If VD.Name = AppName Then
IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
IISAdmin.CommitChanges()
Exit For
End If
can be replaced by:
VB Code:
If VD.Name = AppName Then
VD.DeleteTree()
Exit For
End If
Which I think is much much neater.
Any reason for not using the 2nd method?
Woka
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