列出IIS上的虚拟目录和网站信息。

以前一直想不通怎么用纯WEB来操作IIS(以为必需要装插件),直到发现了很少用到的System.DirectoryServices才豁然开朗。

ContractedBlock.gif ExpandedBlockStart.gif 初探WEB设置IIS
None.gifImports System
None.gif
Imports System.DirectoryServices
None.gif
Imports System.IO
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public Class WebForm5Class WebForm5
InBlock.gif    
Inherits System.Web.UI.Page
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Web 窗体设计器生成的代码#Region " Web 窗体设计器生成的代码 "
InBlock.gif
InBlock.gif    
'该调用是 Web 窗体设计器所必需的。
ExpandedSubBlockStart.gifContractedSubBlock.gif
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif    
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
InBlock.gif    
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
InBlock.gif    
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
InBlock.gif    
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
InBlock.gif
InBlock.gif    
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
InBlock.gif
    '不要删除或移动它。
InBlock.gif
    Private designerPlaceholderDeclaration As System.Object
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InBlock.gif        
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
InBlock.gif
        '不要使用代码编辑器修改它。
InBlock.gif
        InitializeComponent()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InBlock.gif        
'在此处放置初始化页的用户代码
InBlock.gif

InBlock.gif        
If Not Page.IsPostBack Then
InBlock.gif            post1()
InBlock.gif        
End If
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
InBlock.gif
InBlock.gif        
Dim i, ii As Integer
InBlock.gif        
Dim txtDomain As String = ":80:" & Me.TextBox1.Text
InBlock.gif
InBlock.gif        
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC")
InBlock.gif        
For Each root In root.Children
InBlock.gif            
If (root.Name = Request("ID")) And (root.SchemaClassName = "IIsWebServer"Then
InBlock.gif                
For i = 0 To root.Properties("ServerBindings").Count - 1
InBlock.gif                    
If root.Properties("ServerBindings").Item(i) = ":80:" & Trim(Me.TextBox1.Text) Then
InBlock.gif                        
Me.Label1.Text = "主机头已经存在"
InBlock.gif                        root.Close()
InBlock.gif                        
Exit Sub
InBlock.gif                    
End If
InBlock.gif                
Next
InBlock.gif                root.Properties(
"ServerBindings").Add(txtDomain)
InBlock.gif                root.Invoke(
"SetInfo")
InBlock.gif            
End If
InBlock.gif        
Next
InBlock.gif        root.Close()
InBlock.gif        post1()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Function strPath()Function strPath(ByVal siteID As StringAs Object
InBlock.gif        
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC/" & siteID & "/ROOT")
InBlock.gif        strPath 
= root.Properties("Path").Value
InBlock.gif        root.Close()
InBlock.gif        
Return strPath
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Function strWebName()Function strWebName(ByVal webID As StringAs Object
InBlock.gif        
Dim str1, str2 As String
InBlock.gif        
Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC")
InBlock.gif        
For Each root In root.Children
InBlock.gif            
If (root.Name = webID) And (root.SchemaClassName = "IIsWebServer"Then
InBlock.gif                
Dim t, i As Integer
InBlock.gif                
For t = 0 To root.Properties("ServerBindings").Count - 1
InBlock.gif                    str2 
&= "主机头 " & t & " = " & root.Properties("ServerBindings").Item(t) & "<br>"
InBlock.gif                
Next
InBlock.gif                str1 
= root.Properties("ServerComment").Value
InBlock.gif            
End If
InBlock.gif        
Next
InBlock.gif        
Me.Label2.Text = str2
InBlock.gif        root.Close()
InBlock.gif        
Return str1
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub post1()Sub post1()
InBlock.gif        
Dim intID As Integer = Request("ID")
InBlock.gif        Response.Write(
"网站名称= " & strWebName(intID))
InBlock.gif        Response.Write(
"<br>")
InBlock.gif        Response.Write(
"所在路径= " & strPath(intID))
InBlock.gif        Response.Write(
"<p>")
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif


效果如下:
iis.JPG

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值