Imports System.DirectoryServices
Public Class WebDir
Private m_SiteNumber As Integer
Private m_DirName As String
Private m_DirPath As String
Private m_Computer As String
Public Sub New(ByVal DirName As String, ByVal DirPath As String)
m_Computer = "localhost"
m_SiteNumber = 1
m_DirName = DirName
m_DirPath = DirPath
End Sub
Public Sub New(ByVal Computer As String, ByVal SiteNumber As Integer, ByVal DirName As String, ByVal DirPath As String)
m_Computer = Computer
m_SiteNumber = SiteNumber
m_DirPath = DirPath
m_DirName = DirName
End Sub
Public Sub Create()
Try
Dim IISRoot As DirectoryEntry
Dim NewDirectory As DirectoryEntry
IISRoot = New DirectoryEntry("IIS://" & m_Computer & "/W3SVC/" & m_SiteNumber & "/" & "Root")
NewDirectory = IISRoot.Children.Add(m_DirName, "IIsWebVirtualDir")
With NewDirectory
.Properties("Path")(0) = m_DirPath
.CommitChanges()
End With
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Public Sub Delete()
Dim IISRoot As DirectoryEntry
Dim directory As DirectoryEntry
Try
IISRoot = New DirectoryEntry("IIS://" & m_Computer & "/W3SVC/" & m_SiteNumber & "/Root")
directory = IISRoot.Children.Find(m_DirName, "IIsWebVirtualDir")
IISRoot.Children.Remove(directory)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
'returns the IIS service number of the FTP site on the server located at Computer
'returns -1 if could not be found
Public Sub FindSite(ByVal Name As String)
Dim IISRoot As DirectoryEntry
Dim IISRootchildren As DirectoryEntries
Dim site As DirectoryEntry
Try
IISRoot = New DirectoryEntry("IIS://" & m_Computer & "/W3SVC")
IISRootchildren = IISRoot.Children
For Each site In IISRootchildren
If site.SchemaClassName = "IIsWebServer" Then
'check to see if the IIS number matches
If site.Name.ToString() = Name Then
m_SiteNumber = site.Name
Exit Sub
End If
'first check to see if the server comment matches
If site.Properties("ServerComment")(0) = Name Then
m_SiteNumber = site.Name
Exit Sub
End If
'if not, let's look at the IP address
Dim ServerBindings As String
Dim one As String
Dim two As String
Dim ia As String
Dim ip As String
Dim hn As String
ServerBindings = site.Properties("ServerBindings")(0)
one = InStr(ServerBindings, ":")
two = InStr(CType((one + 1), Integer), ServerBindings, ":")
ia = Mid(ServerBindings, 1, (one - 1))
ip = Mid(ServerBindings, (one + 1), ((two - one) - 1))
hn = Mid(ServerBindings, (two + 1))
If ia = Name Then
m_SiteNumber = site.Name
Exit Sub
End If
End If
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
m_SiteNumber = -1
End Sub
End Class