现在的位置: 首页 > 综合 > 正文

VB.Net控制IIS的完整源代码

2013年09月25日 ⁄ 综合 ⁄ 共 14218字 ⁄ 字号 评论关闭

 每段都是一个文件,代码如下:

  1. Imports System
  2. Imports System.DirectoryServices
  3. Imports System.Collections
  4. Namespace OPS.Component
  5.     ' IISWebServer的状态 
  6.     Public Enum IISServerState
  7.         Starting = 1
  8.         Started = 2
  9.         Stopping = 3
  10.         Stopped = 4
  11.         Pausing = 5
  12.         Paused = 6
  13.         Continuing = 7
  14.     End Enum
  15. End Namespace

 

  1. Imports System
  2. Imports System.Collections
  3. Imports System.Collections.Generic
  4. Imports System.Text
  5. Imports System.DirectoryServices
  6. Namespace OPS.Component
  7.     'IISWebServer 
  8.     Public Class IISWebServer
  9.         Friend index As Integer = -1
  10.         Public WebVirtualDirs As IISWebVirtualDirCollection
  11.         ''' <summary> 
  12.         ''' 网站说明 
  13.         ''' </summary> 
  14.         Public ServerComment As String = "Way"
  15.         ''' <summary> 
  16.         ''' 脚本支持 
  17.         ''' </summary> 
  18.         Public AccessScript As Boolean = True
  19.         ''' <summary> 
  20.         ''' 读取 
  21.         ''' </summary> 
  22.         Public AccessRead As Boolean = True
  23.         ''' <summary> 
  24.         ''' 物理路径 
  25.         ''' </summary> 
  26.         Public Path As String = "c:/"
  27.         ''' <summary> 
  28.         ''' 端口 
  29.         ''' </summary> 
  30.         Public Port As Integer = 80
  31.         ''' <summary> 
  32.         ''' 目录浏览 
  33.         ''' </summary> 
  34.         Public EnableDirBrowsing As Boolean = False
  35.         ''' <summary> 
  36.         ''' 默认文档 
  37.         ''' </summary> 
  38.         Public DefaultDoc As String = "index.aspx"
  39.         ''' <summary> 
  40.         ''' 使用默认文档 
  41.         ''' </summary> 
  42.         Public EnableDefaultDoc As Boolean = True
  43.         ''' <summary> 
  44.         ''' IISWebServer的状态 
  45.         ''' </summary> 
  46.         Public ReadOnly Property ServerState() As IISServerState
  47.             Get
  48.                 Dim server As DirectoryEntry = IISManagement.returnIISWebserver(Me.index)
  49.                 If server Is Nothing Then
  50.                     Throw (New Exception("找不到此IISWebServer"))
  51.                 End If
  52.                 Select Case server.Properties("ServerState")(0).ToString()
  53.                     Case "2"
  54.                         Return IISServerState.Started
  55.                     Case "4"
  56.                         Return IISServerState.Stopped
  57.                     Case "6"
  58.                         Return IISServerState.Paused
  59.                 End Select
  60.                 Return IISServerState.Stopped
  61.             End Get
  62.         End Property
  63.         ''' <summary> 
  64.         ''' 停止IISWebServer 
  65.         ''' </summary> 
  66.         Public Sub [Stop]()
  67.             Dim Server As DirectoryEntry
  68.             If index = -1 Then
  69.                 Throw (New Exception("在IIS找不到此IISWebServer!"))
  70.             End If
  71.             Try
  72.                 Server = New DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index)
  73.                 If Server IsNot Nothing Then
  74.                     Server.Invoke("stop"New Object(-1) {})
  75.                 Else
  76.                     Throw (New Exception("在IIS找不到此IISWebServer!"))
  77.                 End If
  78.             Catch
  79.                 Throw (New Exception("在IIS找不到此IISWebServer!"))
  80.             End Try
  81.         End Sub
  82.         ''' <summary> 
  83.         ''' 把基本信息的更改更新到IIS 
  84.         ''' </summary> 
  85.         Public Sub CommitChanges()
  86.             IISManagement.EditIISWebServer(Me)
  87.         End Sub
  88.         ''' <summary> 
  89.         ''' 启动IISWebServer 
  90.         ''' </summary> 
  91.         Public Sub Start()
  92.             If index = -1 Then
  93.                 Throw (New Exception("在IIS找不到此IISWebServer!"))
  94.             End If
  95.             Dim Service As New DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC")
  96.             Dim Server As DirectoryEntry
  97.             Dim ie As IEnumerator = Service.Children.GetEnumerator()
  98.             While ie.MoveNext()
  99.                 Server = DirectCast(ie.Current, DirectoryEntry)
  100.                 If Server.SchemaClassName = "IIsWebServer" Then
  101.                     If Server.Properties("Serverbindings")(0).ToString() = ":" + Me.Port + ":" Then
  102.                         Server.Invoke("stop"New Object(-1) {})
  103.                     End If
  104.                 End If
  105.             End While
  106.             Try
  107.                 Server = New DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index)
  108.                 If Server IsNot Nothing Then
  109.                     Server.Invoke("start"New Object(-1) {})
  110.                 Else
  111.                     Throw (New Exception("在IIS找不到此IISWebServer!"))
  112.                 End If
  113.             Catch
  114.                 Throw (New Exception("在IIS找不到此IISWebServer!"))
  115.             End Try
  116.         End Sub
  117.         Public Sub New()
  118.             WebVirtualDirs = New IISWebVirtualDirCollection(Me)
  119.         End Sub
  120.     End Class
  121. End Namespace

 

 

  1. Imports System
  2. Imports System.Collections
  3. Imports System.Collections.Generic
  4. Imports System.Text
  5. Namespace OPS.Component
  6.     ''' <summary> 
  7.     ''' IISWebServerCollection 
  8.     ''' </summary> 
  9.     Public Class IISWebServerCollection
  10.         Inherits CollectionBase
  11.         Default Public ReadOnly Property Item(ByVal Index As IntegerAs IISWebServer
  12.             Get
  13.                 Return DirectCast(Me.List(Index), IISWebServer)
  14.             End Get
  15.         End Property
  16.         Default Public ReadOnly Property Item(ByVal ServerComment As StringAs IISWebServer
  17.             Get
  18.                 ServerComment = ServerComment.ToLower().Trim()
  19.                 Dim list As IISWebServer
  20.                 For i As Integer = 0 To Me.List.Count - 1
  21.                     list = DirectCast(Me.List(i), IISWebServer)
  22.                     If list.ServerComment.ToLower().Trim() = ServerComment Then
  23.                         Return list
  24.                     End If
  25.                 Next
  26.                 Return Nothing
  27.             End Get
  28.         End Property
  29.         Friend Sub Add_(ByVal WebServer As IISWebServer)
  30.             Me.List.Add(WebServer)
  31.         End Sub
  32.         Public Sub Add(ByVal WebServer As IISWebServer)
  33.             Try
  34.                 Me.List.Add(WebServer)
  35.                 IISManagement.CreateIISWebServer(WebServer)
  36.             Catch
  37.                 Throw (New Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
  38.             End Try
  39.         End Sub
  40.         ''' <summary> 
  41.         ''' 是否包含指定的网站 
  42.         ''' </summary> 
  43.         ''' <param name="ServerComment"></param> 
  44.         ''' <returns></returns> 
  45.         Public Function Contains(ByVal ServerComment As StringAs Boolean
  46.             ServerComment = ServerComment.ToLower().Trim()
  47.             For i As Integer = 0 To Me.List.Count - 1
  48.                 Dim server As IISWebServer = Me(i)
  49.                 If server.ServerComment.ToLower().Trim() = ServerComment Then
  50.                     Return True
  51.                 End If
  52.             Next
  53.             Return False
  54.         End Function
  55.         ''' <summary> 
  56.         ''' 是否包含指定的网站 
  57.         ''' </summary> 
  58.         ''' <param name="index"></param> 
  59.         ''' <returns></returns> 
  60.         Public Function Contains(ByVal index As IntegerAs Boolean
  61.             For i As Integer = 0 To Me.List.Count - 1
  62.                 Dim server As IISWebServer = Me(i)
  63.                 If server.index = index Then
  64.                     Return True
  65.                 End If
  66.             Next
  67.             Return False
  68.         End Function
  69.         Public Sub AddRange(ByVal WebServers As IISWebServer())
  70.             For i As Integer = 0 To WebServers.GetUpperBound(0)
  71.                 Add(WebServers(i))
  72.             Next
  73.         End Sub
  74.         Public Sub Remove(ByVal WebServer As IISWebServer)
  75.             For i As Integer = 0 To Me.List.Count - 1
  76.                 If DirectCast(Me.List(i), IISWebServer) = WebServer Then
  77.                     Me.List.RemoveAt(i)
  78.                     Return
  79.                 End If
  80.             Next
  81.             IISManagement.RemoveIISWebServer(WebServer.index)
  82.         End Sub
  83.     End Class
  84. End Namespace

 

 

  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Text
  4. Namespace OPS.Component
  5.     ''' <summary> 
  6.     ''' IISWebVirtualDir 
  7.     ''' </summary> 
  8.     Public Class IISWebVirtualDir
  9.         Public Parent As IISWebServer = Nothing
  10.         ''' <summary> 
  11.         ''' 虚拟目录名称 
  12.         ''' </summary> 
  13.         Public Name As String = "Way"
  14.         ''' <summary> 
  15.         ''' 读取 
  16.         ''' </summary> 
  17.         Public AccessRead As Boolean = True
  18.         ''' <summary> 
  19.         ''' 脚本支持 
  20.         ''' </summary> 
  21.         Public AccessScript As Boolean = True
  22.         ''' <summary> 
  23.         ''' 物理路径 
  24.         ''' </summary> 
  25.         Public Path As String = "c:/"
  26.         ''' <summary> 
  27.         ''' 默认文档 
  28.         ''' </summary> 
  29.         Public DefaultDoc As String = "index.aspx"
  30.         ''' <summary> 
  31.         ''' 使用默认文档 
  32.         ''' </summary> 
  33.         Public EnableDefaultDoc As Boolean = True
  34.         ''' <summary> 
  35.         ''' 所属的网站的网站说明 
  36.         ''' </summary> 
  37.         Public WebServer As String = ""
  38.         Public Sub New(ByVal WebServerName As String)
  39.             If WebServerName.ToString() = "" Then
  40.                 Throw (New Exception("WebServerName不能为空!"))
  41.             End If
  42.             Me.WebServer = WebServerName
  43.         End Sub
  44.         Public Sub New()
  45.         End Sub
  46.     End Class
  47. End Namespace

 

 

  1. Imports System
  2. Imports System.Collections
  3. Imports System.Collections.Generic
  4. Imports System.Text
  5. Namespace OPS.Component
  6.     ''' <summary> 
  7.     ''' IISWebVirtualDirCollection 
  8.     ''' </summary> 
  9.     Public Class IISWebVirtualDirCollection
  10.         Inherits CollectionBase
  11.         Public Parent As IISWebServer = Nothing
  12.         Default Public ReadOnly Property Item(ByVal Index As IntegerAs IISWebVirtualDir
  13.             Get
  14.                 Return DirectCast(Me.List(Index), IISWebVirtualDir)
  15.             End Get
  16.         End Property
  17.         Default Public ReadOnly Property Item(ByVal Name As StringAs IISWebVirtualDir
  18.             Get
  19.                 Name = Name.ToLower()
  20.                 Dim list As IISWebVirtualDir
  21.                 For i As Integer = 0 To Me.List.Count - 1
  22.                     list = DirectCast(Me.List(i), IISWebVirtualDir)
  23.                     If list.Name.ToLower() = Name Then
  24.                         Return list
  25.                     End If
  26.                 Next
  27.                 Return Nothing
  28.             End Get
  29.         End Property
  30.         Friend Sub Add_(ByVal WebVirtualDir As IISWebVirtualDir)
  31.             Try
  32.                 Me.List.Add(WebVirtualDir)
  33.             Catch
  34.                 Throw (New Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
  35.             End Try
  36.         End Sub
  37.         Public Sub Add(ByVal WebVirtualDir As IISWebVirtualDir)
  38.             WebVirtualDir.Parent = Me.Parent
  39.             Try
  40.                 Me.List.Add(WebVirtualDir)
  41.             Catch
  42.                 Throw (New Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
  43.             End Try
  44.             IISManagement.CreateIISWebVirtualDir(WebVirtualDir, True)
  45.         End Sub
  46.         Public Sub AddRange(ByVal WebVirtualDirs As IISWebVirtualDir())
  47.             For i As Integer = 0 To WebVirtualDirs.GetUpperBound(0)
  48.                 Add(WebVirtualDirs(i))
  49.             Next
  50.         End Sub
  51.         Public Sub Remove(ByVal WebVirtualDir As IISWebVirtualDir)
  52.             For i As Integer = 0 To Me.List.Count - 1
  53.                 If DirectCast(Me.List(i), IISWebVirtualDir) = WebVirtualDir Then
  54.                     Me.List.RemoveAt(i)
  55.                     IISManagement.RemoveIISWebVirtualDir(WebVirtualDir)
  56.                     Return
  57.                 End If
  58.             Next
  59.         End Sub
  60.         Public Sub New(ByVal Parent As IISWebServer)
  61.             Me.Parent = Parent
  62.         End Sub
  63.     End Class
  64. End Namespace

 

 

  1. Imports System
  2. Imports System.Collections
  3. Imports System.Collections.Generic
  4. Imports System.Text
  5. Imports System.DirectoryServices
  6. Namespace OPS.Component
  7.     ''' <summary> 
  8.     ''' IISManagement 的摘要说明。
  9.     ''' </summary> 
  10.     Public Class IISManagement
  11.         Public WebServers As New IISWebServerCollection()
  12.         Friend Shared Machinename As String = "localhost"
  13.         Public Sub New()
  14.             start()
  15.         End Sub
  16.         ''' <param name="MachineName">机器名,默认值为localhost</param> 
  17.         Public Sub New(ByVal MachineName__1 As String)
  18.             If MachineName__1.ToString() <> "" Then
  19.                 Machinename = MachineName__1
  20.             End If
  21.             start()
  22.         End Sub
  23.         Private Sub start()
  24.             Dim Service As New DirectoryEntry("IIS://" + Machinename + "/W3SVC")
  25.             Dim Server As DirectoryEntry
  26.             Dim Root As DirectoryEntry = Nothing
  27.             Dim VirDir As DirectoryEntry
  28.             Dim ie As IEnumerator = Service.Children.GetEnumerator()
  29.             Dim ieRoot As IEnumerator
  30.             Dim item As IISWebServer
  31.             Dim item_virdir As IISWebVirtualDir
  32.             Dim finded As Boolean = False
  33.             While ie.MoveNext()
  34.                 Server = DirectCast(ie.Current, DirectoryEntry)
  35.                 If Server.SchemaClassName = "IIsWebServer" Then
  36.                     item = New IISWebServer()
  37.                     item.index = Convert.ToInt32(Server.Name)
  38.                     item.ServerComment = DirectCast(Server.Properties("ServerComment")(0), String)
  39.                     item.AccessRead = CBool(Server.Properties("AccessRead")(0))
  40.                     item.AccessScript = CBool(Server.Properties("AccessScript")(0))
  41.                     item.DefaultDoc = DirectCast(Server.Properties("DefaultDoc")(0), String)
  42.                     item.EnableDefaultDoc = CBool(Server.Properties("EnableDefaultDoc")(0))
  43.                     item.EnableDirBrowsing = CBool(Server.Properties("EnableDirBrowsing")(0))
  44.                     ieRoot = Server.Children.GetEnumerator()
  45.                     While ieRoot.MoveNext()
  46.                         Root = DirectCast(ieRoot.Current, DirectoryEntry)
  47.                         If Root.SchemaClassName = "IIsWebVirtualDir" Then
  48.                             finded = True
  49.                             Exit While
  50.                         End If
  51.                     End While
  52.                     If finded Then
  53.                         item.Path = Root.Properties("path")(0).ToString()
  54.                     End If
  55.                     item.Port = Convert.ToInt32(DirectCast(Server.Properties("Serverbindings")(0), String).Substring(1, (DirectCast(Server.Properties("Serverbindings")(0), String)).Length - 2))
  56.                     Me.WebServers.Add_(item)
  57.                     ieRoot = Root.Children.GetEnumerator()
  58.                     While ieRoot.MoveNext()
  59.                         VirDir = DirectCast(ieRoot.Current, DirectoryEntry)
  60.                         If VirDir.SchemaClassName <> "IIsWebVirtualDir" AndAlso VirDir.SchemaClassName <> "IIsWebDirectory" Then
  61.                             Continue While
  62.                         End If

抱歉!评论已关闭.