以前一直想不通怎么用纯WEB来操作IIS(以为必需要装插件),直到发现了很少用到的System.DirectoryServices才豁然开朗。
初探WEB设置IIS
Imports System
Imports System.DirectoryServices
Imports System.IO
Public Class WebForm5Class WebForm5
Inherits System.Web.UI.Page
Web 窗体设计器生成的代码#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not Page.IsPostBack Then
post1()
End If
End Sub
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, ii As Integer
Dim txtDomain As String = ":80:" & Me.TextBox1.Text
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC")
For Each root In root.Children
If (root.Name = Request("ID")) And (root.SchemaClassName = "IIsWebServer") Then
For i = 0 To root.Properties("ServerBindings").Count - 1
If root.Properties("ServerBindings").Item(i) = ":80:" & Trim(Me.TextBox1.Text) Then
Me.Label1.Text = "主机头已经存在"
root.Close()
Exit Sub
End If
Next
root.Properties("ServerBindings").Add(txtDomain)
root.Invoke("SetInfo")
End If
Next
root.Close()
post1()
End Sub
Private Function strPath()Function strPath(ByVal siteID As String) As Object
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC/" & siteID & "/ROOT")
strPath = root.Properties("Path").Value
root.Close()
Return strPath
End Function
Private Function strWebName()Function strWebName(ByVal webID As String) As Object
Dim str1, str2 As String
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC")
For Each root In root.Children
If (root.Name = webID) And (root.SchemaClassName = "IIsWebServer") Then
Dim t, i As Integer
For t = 0 To root.Properties("ServerBindings").Count - 1
str2 &= "主机头 " & t & " = " & root.Properties("ServerBindings").Item(t) & "<br>"
Next
str1 = root.Properties("ServerComment").Value
End If
Next
Me.Label2.Text = str2
root.Close()
Return str1
End Function
Private Sub post1()Sub post1()
Dim intID As Integer = Request("ID")
Response.Write("网站名称= " & strWebName(intID))
Response.Write("<br>")
Response.Write("所在路径= " & strPath(intID))
Response.Write("<p>")
End Sub
End Class
Imports System
Imports System.DirectoryServices
Imports System.IO
Public Class WebForm5Class WebForm5
Inherits System.Web.UI.Page
Web 窗体设计器生成的代码#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not Page.IsPostBack Then
post1()
End If
End Sub
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, ii As Integer
Dim txtDomain As String = ":80:" & Me.TextBox1.Text
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC")
For Each root In root.Children
If (root.Name = Request("ID")) And (root.SchemaClassName = "IIsWebServer") Then
For i = 0 To root.Properties("ServerBindings").Count - 1
If root.Properties("ServerBindings").Item(i) = ":80:" & Trim(Me.TextBox1.Text) Then
Me.Label1.Text = "主机头已经存在"
root.Close()
Exit Sub
End If
Next
root.Properties("ServerBindings").Add(txtDomain)
root.Invoke("SetInfo")
End If
Next
root.Close()
post1()
End Sub
Private Function strPath()Function strPath(ByVal siteID As String) As Object
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC/" & siteID & "/ROOT")
strPath = root.Properties("Path").Value
root.Close()
Return strPath
End Function
Private Function strWebName()Function strWebName(ByVal webID As String) As Object
Dim str1, str2 As String
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC")
For Each root In root.Children
If (root.Name = webID) And (root.SchemaClassName = "IIsWebServer") Then
Dim t, i As Integer
For t = 0 To root.Properties("ServerBindings").Count - 1
str2 &= "主机头 " & t & " = " & root.Properties("ServerBindings").Item(t) & "<br>"
Next
str1 = root.Properties("ServerComment").Value
End If
Next
Me.Label2.Text = str2
root.Close()
Return str1
End Function
Private Sub post1()Sub post1()
Dim intID As Integer = Request("ID")
Response.Write("网站名称= " & strWebName(intID))
Response.Write("<br>")
Response.Write("所在路径= " & strPath(intID))
Response.Write("<p>")
End Sub
End Class
效果如下: