每段都是一个文件,代码如下:
- Imports System
- Imports System.DirectoryServices
- Imports System.Collections
- Namespace OPS.Component
- ' IISWebServer的状态
- Public Enum IISServerState
- Starting = 1
- Started = 2
- Stopping = 3
- Stopped = 4
- Pausing = 5
- Paused = 6
- Continuing = 7
- End Enum
- End Namespace
- Imports System
- Imports System.Collections
- Imports System.Collections.Generic
- Imports System.Text
- Imports System.DirectoryServices
- Namespace OPS.Component
- 'IISWebServer
- Public Class IISWebServer
- Friend index As Integer = -1
- Public WebVirtualDirs As IISWebVirtualDirCollection
- ''' <summary>
- ''' 网站说明
- ''' </summary>
- Public ServerComment As String = "Way"
- ''' <summary>
- ''' 脚本支持
- ''' </summary>
- Public AccessScript As Boolean = True
- ''' <summary>
- ''' 读取
- ''' </summary>
- Public AccessRead As Boolean = True
- ''' <summary>
- ''' 物理路径
- ''' </summary>
- Public Path As String = "c:/"
- ''' <summary>
- ''' 端口
- ''' </summary>
- Public Port As Integer = 80
- ''' <summary>
- ''' 目录浏览
- ''' </summary>
- Public EnableDirBrowsing As Boolean = False
- ''' <summary>
- ''' 默认文档
- ''' </summary>
- Public DefaultDoc As String = "index.aspx"
- ''' <summary>
- ''' 使用默认文档
- ''' </summary>
- Public EnableDefaultDoc As Boolean = True
- ''' <summary>
- ''' IISWebServer的状态
- ''' </summary>
- Public ReadOnly Property ServerState() As IISServerState
- Get
- Dim server As DirectoryEntry = IISManagement.returnIISWebserver(Me.index)
- If server Is Nothing Then
- Throw (New Exception("找不到此IISWebServer"))
- End If
- Select Case server.Properties("ServerState")(0).ToString()
- Case "2"
- Return IISServerState.Started
- Case "4"
- Return IISServerState.Stopped
- Case "6"
- Return IISServerState.Paused
- End Select
- Return IISServerState.Stopped
- End Get
- End Property
- ''' <summary>
- ''' 停止IISWebServer
- ''' </summary>
- Public Sub [Stop]()
- Dim Server As DirectoryEntry
- If index = -1 Then
- Throw (New Exception("在IIS找不到此IISWebServer!"))
- End If
- Try
- Server = New DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index)
- If Server IsNot Nothing Then
- Server.Invoke("stop", New Object(-1) {})
- Else
- Throw (New Exception("在IIS找不到此IISWebServer!"))
- End If
- Catch
- Throw (New Exception("在IIS找不到此IISWebServer!"))
- End Try
- End Sub
- ''' <summary>
- ''' 把基本信息的更改更新到IIS
- ''' </summary>
- Public Sub CommitChanges()
- IISManagement.EditIISWebServer(Me)
- End Sub
- ''' <summary>
- ''' 启动IISWebServer
- ''' </summary>
- Public Sub Start()
- If index = -1 Then
- Throw (New Exception("在IIS找不到此IISWebServer!"))
- End If
- Dim Service As New DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC")
- Dim Server As DirectoryEntry
- Dim ie As IEnumerator = Service.Children.GetEnumerator()
- While ie.MoveNext()
- Server = DirectCast(ie.Current, DirectoryEntry)
- If Server.SchemaClassName = "IIsWebServer" Then
- If Server.Properties("Serverbindings")(0).ToString() = ":" + Me.Port + ":" Then
- Server.Invoke("stop", New Object(-1) {})
- End If
- End If
- End While
- Try
- Server = New DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index)
- If Server IsNot Nothing Then
- Server.Invoke("start", New Object(-1) {})
- Else
- Throw (New Exception("在IIS找不到此IISWebServer!"))
- End If
- Catch
- Throw (New Exception("在IIS找不到此IISWebServer!"))
- End Try
- End Sub
- Public Sub New()
- WebVirtualDirs = New IISWebVirtualDirCollection(Me)
- End Sub
- End Class
- End Namespace
- Imports System
- Imports System.Collections
- Imports System.Collections.Generic
- Imports System.Text
- Namespace OPS.Component
- ''' <summary>
- ''' IISWebServerCollection
- ''' </summary>
- Public Class IISWebServerCollection
- Inherits CollectionBase
- Default Public ReadOnly Property Item(ByVal Index As Integer) As IISWebServer
- Get
- Return DirectCast(Me.List(Index), IISWebServer)
- End Get
- End Property
- Default Public ReadOnly Property Item(ByVal ServerComment As String) As IISWebServer
- Get
- ServerComment = ServerComment.ToLower().Trim()
- Dim list As IISWebServer
- For i As Integer = 0 To Me.List.Count - 1
- list = DirectCast(Me.List(i), IISWebServer)
- If list.ServerComment.ToLower().Trim() = ServerComment Then
- Return list
- End If
- Next
- Return Nothing
- End Get
- End Property
- Friend Sub Add_(ByVal WebServer As IISWebServer)
- Me.List.Add(WebServer)
- End Sub
- Public Sub Add(ByVal WebServer As IISWebServer)
- Try
- Me.List.Add(WebServer)
- IISManagement.CreateIISWebServer(WebServer)
- Catch
- Throw (New Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
- End Try
- End Sub
- ''' <summary>
- ''' 是否包含指定的网站
- ''' </summary>
- ''' <param name="ServerComment"></param>
- ''' <returns></returns>
- Public Function Contains(ByVal ServerComment As String) As Boolean
- ServerComment = ServerComment.ToLower().Trim()
- For i As Integer = 0 To Me.List.Count - 1
- Dim server As IISWebServer = Me(i)
- If server.ServerComment.ToLower().Trim() = ServerComment Then
- Return True
- End If
- Next
- Return False
- End Function
- ''' <summary>
- ''' 是否包含指定的网站
- ''' </summary>
- ''' <param name="index"></param>
- ''' <returns></returns>
- Public Function Contains(ByVal index As Integer) As Boolean
- For i As Integer = 0 To Me.List.Count - 1
- Dim server As IISWebServer = Me(i)
- If server.index = index Then
- Return True
- End If
- Next
- Return False
- End Function
- Public Sub AddRange(ByVal WebServers As IISWebServer())
- For i As Integer = 0 To WebServers.GetUpperBound(0)
- Add(WebServers(i))
- Next
- End Sub
- Public Sub Remove(ByVal WebServer As IISWebServer)
- For i As Integer = 0 To Me.List.Count - 1
- If DirectCast(Me.List(i), IISWebServer) = WebServer Then
- Me.List.RemoveAt(i)
- Return
- End If
- Next
- IISManagement.RemoveIISWebServer(WebServer.index)
- End Sub
- End Class
- End Namespace
- Imports System
- Imports System.Collections.Generic
- Imports System.Text
- Namespace OPS.Component
- ''' <summary>
- ''' IISWebVirtualDir
- ''' </summary>
- Public Class IISWebVirtualDir
- Public Parent As IISWebServer = Nothing
- ''' <summary>
- ''' 虚拟目录名称
- ''' </summary>
- Public Name As String = "Way"
- ''' <summary>
- ''' 读取
- ''' </summary>
- Public AccessRead As Boolean = True
- ''' <summary>
- ''' 脚本支持
- ''' </summary>
- Public AccessScript As Boolean = True
- ''' <summary>
- ''' 物理路径
- ''' </summary>
- Public Path As String = "c:/"
- ''' <summary>
- ''' 默认文档
- ''' </summary>
- Public DefaultDoc As String = "index.aspx"
- ''' <summary>
- ''' 使用默认文档
- ''' </summary>
- Public EnableDefaultDoc As Boolean = True
- ''' <summary>
- ''' 所属的网站的网站说明
- ''' </summary>
- Public WebServer As String = ""
- Public Sub New(ByVal WebServerName As String)
- If WebServerName.ToString() = "" Then
- Throw (New Exception("WebServerName不能为空!"))
- End If
- Me.WebServer = WebServerName
- End Sub
- Public Sub New()
- End Sub
- End Class
- End Namespace
- Imports System
- Imports System.Collections
- Imports System.Collections.Generic
- Imports System.Text
- Namespace OPS.Component
- ''' <summary>
- ''' IISWebVirtualDirCollection
- ''' </summary>
- Public Class IISWebVirtualDirCollection
- Inherits CollectionBase
- Public Parent As IISWebServer = Nothing
- Default Public ReadOnly Property Item(ByVal Index As Integer) As IISWebVirtualDir
- Get
- Return DirectCast(Me.List(Index), IISWebVirtualDir)
- End Get
- End Property
- Default Public ReadOnly Property Item(ByVal Name As String) As IISWebVirtualDir
- Get
- Name = Name.ToLower()
- Dim list As IISWebVirtualDir
- For i As Integer = 0 To Me.List.Count - 1
- list = DirectCast(Me.List(i), IISWebVirtualDir)
- If list.Name.ToLower() = Name Then
- Return list
- End If
- Next
- Return Nothing
- End Get
- End Property
- Friend Sub Add_(ByVal WebVirtualDir As IISWebVirtualDir)
- Try
- Me.List.Add(WebVirtualDir)
- Catch
- Throw (New Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
- End Try
- End Sub
- Public Sub Add(ByVal WebVirtualDir As IISWebVirtualDir)
- WebVirtualDir.Parent = Me.Parent
- Try
- Me.List.Add(WebVirtualDir)
- Catch
- Throw (New Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
- End Try
- IISManagement.CreateIISWebVirtualDir(WebVirtualDir, True)
- End Sub
- Public Sub AddRange(ByVal WebVirtualDirs As IISWebVirtualDir())
- For i As Integer = 0 To WebVirtualDirs.GetUpperBound(0)
- Add(WebVirtualDirs(i))
- Next
- End Sub
- Public Sub Remove(ByVal WebVirtualDir As IISWebVirtualDir)
- For i As Integer = 0 To Me.List.Count - 1
- If DirectCast(Me.List(i), IISWebVirtualDir) = WebVirtualDir Then
- Me.List.RemoveAt(i)
- IISManagement.RemoveIISWebVirtualDir(WebVirtualDir)
- Return
- End If
- Next
- End Sub
- Public Sub New(ByVal Parent As IISWebServer)
- Me.Parent = Parent
- End Sub
- End Class
- End Namespace
- Imports System
- Imports System.Collections
- Imports System.Collections.Generic
- Imports System.Text
- Imports System.DirectoryServices
- Namespace OPS.Component
- ''' <summary>
- ''' IISManagement 的摘要说明。
- ''' </summary>
- Public Class IISManagement
- Public WebServers As New IISWebServerCollection()
- Friend Shared Machinename As String = "localhost"
- Public Sub New()
- start()
- End Sub
- ''' <param name="MachineName">机器名,默认值为localhost</param>
- Public Sub New(ByVal MachineName__1 As String)
- If MachineName__1.ToString() <> "" Then
- Machinename = MachineName__1
- End If
- start()
- End Sub
- Private Sub start()
- Dim Service As New DirectoryEntry("IIS://" + Machinename + "/W3SVC")
- Dim Server As DirectoryEntry
- Dim Root As DirectoryEntry = Nothing
- Dim VirDir As DirectoryEntry
- Dim ie As IEnumerator = Service.Children.GetEnumerator()
- Dim ieRoot As IEnumerator
- Dim item As IISWebServer
- Dim item_virdir As IISWebVirtualDir
- Dim finded As Boolean = False
- While ie.MoveNext()
- Server = DirectCast(ie.Current, DirectoryEntry)
- If Server.SchemaClassName = "IIsWebServer" Then
- item = New IISWebServer()
- item.index = Convert.ToInt32(Server.Name)
- item.ServerComment = DirectCast(Server.Properties("ServerComment")(0), String)
- item.AccessRead = CBool(Server.Properties("AccessRead")(0))
- item.AccessScript = CBool(Server.Properties("AccessScript")(0))
- item.DefaultDoc = DirectCast(Server.Properties("DefaultDoc")(0), String)
- item.EnableDefaultDoc = CBool(Server.Properties("EnableDefaultDoc")(0))
- item.EnableDirBrowsing = CBool(Server.Properties("EnableDirBrowsing")(0))
- ieRoot = Server.Children.GetEnumerator()
- While ieRoot.MoveNext()
- Root = DirectCast(ieRoot.Current, DirectoryEntry)
- If Root.SchemaClassName = "IIsWebVirtualDir" Then
- finded = True
- Exit While
- End If
- End While
- If finded Then
- item.Path = Root.Properties("path")(0).ToString()
- End If
- item.Port = Convert.ToInt32(DirectCast(Server.Properties("Serverbindings")(0), String).Substring(1, (DirectCast(Server.Properties("Serverbindings")(0), String)).Length - 2))
- Me.WebServers.Add_(item)
- ieRoot = Root.Children.GetEnumerator()
- While ieRoot.MoveNext()
- VirDir = DirectCast(ieRoot.Current, DirectoryEntry)
- If VirDir.SchemaClassName <> "IIsWebVirtualDir" AndAlso VirDir.SchemaClassName <> "IIsWebDirectory" Then
- Continue While
- End If