前端之家收集整理的这篇文章主要介绍了
VB.Net控制IIS的完整源代码,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每段都是一个文件,代码如下:
- ImportsSystem
- ImportsSystem.DirectoryServices
- ImportsSystem.Collections
-
- NamespaceOPS.Component
-
- PublicEnumIISServerState
- Starting=1
- Started=2
- Stopping=3
- Stopped=4
- Pausing=5
- Paused=6
- Continuing=7
- EndEnum
- EndNamespace
- ImportsSystem
- ImportsSystem.Collections
- ImportsSystem.Collections.Generic
- ImportsSystem.Text
-
- NamespaceOPS.Component
-
-
-
- PublicClassIISWebServerCollection
- InheritsCollectionBase
- DefaultPublicReadOnlyPropertyItem(ByValIndexAsInteger)AsIISWebServer
- Get
- ReturnDirectCast(Me.List(Index),IISWebServer)
-
- EndGet
- EndProperty
-
- DefaultPublicReadOnlyPropertyItem(ByValServerCommentAsString)AsIISWebServer
- Get
- ServerComment=ServerComment.ToLower().Trim()
- DimlistAsIISWebServer
- ForiAsInteger=0ToMe.List.Count-1
- list=DirectCast(Me.List(i),IISWebServer)
- Iflist.ServerComment.ToLower().Trim()=ServerCommentThen
- Returnlist
- EndIf
- Next
- ReturnNothing
- EndGet
- EndProperty
-
- FriendSubAdd_(ByValWebServerAsIISWebServer)
- Me.List.Add(WebServer)
- EndSub
- PublicSubAdd(ByValWebServerAsIISWebServer)
- Try
- Me.List.Add(WebServer)
- IISManagement.CreateIISWebServer(WebServer)
- Catch
- Throw(NewException("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
- EndTry
-
- EndSub
-
-
-
-
-
-
- PublicFunctionContains(ByValServerCommentAsString)AsBoolean
- ServerComment=ServerComment.ToLower().Trim()
- ForiAsInteger=0ToMe.List.Count-1
- DimserverAsIISWebServer=Me(i)
- Ifserver.ServerComment.ToLower().Trim()=ServerCommentThen
- ReturnTrue
- EndIf
- Next
- ReturnFalse
- EndFunction
-
-
-
-
-
-
- PublicFunctionContains(ByValindexAsInteger)AsBoolean
- ForiAsInteger=0ToMe.List.Count-1
- DimserverAsIISWebServer=Me(i)
- Ifserver.index=indexThen
- ReturnTrue
- EndIf
- Next
- ReturnFalse
- EndFunction
-
- PublicSubAddRange(ByValWebServersAsIISWebServer())
- ForiAsInteger=0ToWebServers.GetUpperBound(0)
- Add(WebServers(i))
- Next
- EndSub
-
- PublicSubRemove(ByValWebServerAsIISWebServer)
- ForiAsInteger=0ToMe.List.Count-1
- IfDirectCast(Me.List(i),IISWebServer)=WebServerThen
- Me.List.RemoveAt(i)
- Return
- EndIf
- Next
- IISManagement.RemoveIISWebServer(WebServer.index)
- EndSub
- EndClass
- EndNamespace
- ImportsSystem
- ImportsSystem.Collections.Generic
- ImportsSystem.Text
-
- NamespaceOPS.Component
-
-
-
- PublicClassIISWebVirtualDir
- PublicParentAsIISWebServer=Nothing
-
-
-
- PublicNameAsString="Way"
-
-
-
- PublicAccessReadAsBoolean=True
-
-
-
- PublicAccessScriptAsBoolean=True
-
-
-
- PublicPathAsString="c:/"
-
-
-
- PublicDefaultDocAsString="index.aspx"
-
-
-
- PublicEnableDefaultDocAsBoolean=True
-
-
-
- PublicWebServerAsString=""
- PublicSubNew(ByValWebServerNameAsString)
- IfWebServerName.ToString()=""Then
- Throw(NewException("WebServerName不能为空!"))
- EndIf
- Me.WebServer=WebServerName
- EndSub
-
- PublicSubNew()
-
- EndSub
- EndClass
- EndNamespace
- ImportsSystem
- ImportsSystem.Collections
- ImportsSystem.Collections.Generic
- ImportsSystem.Text
-
- NamespaceOPS.Component
-
-
-
- PublicClassIISWebVirtualDirCollection
- InheritsCollectionBase
-
- PublicParentAsIISWebServer=Nothing
- DefaultPublicReadOnlyPropertyItem(ByValIndexAsInteger)AsIISWebVirtualDir
- Get
- ReturnDirectCast(Me.List(Index),IISWebVirtualDir)
-
- EndGet
- EndProperty
-
- DefaultPublicReadOnlyPropertyItem(ByValNameAsString)AsIISWebVirtualDir
- Get
- Name=Name.ToLower()
- DimlistAsIISWebVirtualDir
- ForiAsInteger=0ToMe.List.Count-1
- list=DirectCast(Me.List(i),IISWebVirtualDir)
- Iflist.Name.ToLower()=NameThen
- Returnlist
- EndIf
- Next
- ReturnNothing
- EndGet
- EndProperty
-
- FriendSubAdd_(ByValWebVirtualDirAsIISWebVirtualDir)
- Try
- Me.List.Add(WebVirtualDir)
- Catch
- Throw(NewException("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
- EndTry
-
- EndSub
-
- PublicSubAdd(ByValWebVirtualDirAsIISWebVirtualDir)
- WebVirtualDir.Parent=Me.Parent
- Try
- Me.List.Add(WebVirtualDir)
-
- Catch
- Throw(NewException("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"))
- EndTry
- IISManagement.CreateIISWebVirtualDir(WebVirtualDir,True)
-
- EndSub
-
- PublicSubAddRange(ByValWebVirtualDirsAsIISWebVirtualDir())
- ForiAsInteger=0ToWebVirtualDirs.GetUpperBound(0)
- Add(WebVirtualDirs(i))
- Next
- EndSub
-
- PublicSubRemove(ByValWebVirtualDirAsIISWebVirtualDir)
- ForiAsInteger=0ToMe.List.Count-1
- IfDirectCast(Me.List(i),IISWebVirtualDir)=WebVirtualDirThen
- Me.List.RemoveAt(i)
- IISManagement.RemoveIISWebVirtualDir(WebVirtualDir)
- Return
- EndIf
- Next
- EndSub
-
- PublicSubNew(ByValParentAsIISWebServer)
- Me.Parent=Parent
- EndSub
- EndClass
- EndNamespace
- ImportsSystem
- ImportsSystem.Collections
- ImportsSystem.Collections.Generic
- ImportsSystem.Text
- ImportsSystem.DirectoryServices
-
- NamespaceOPS.Component
-
-
-
- PublicClassIISManagement
- PublicWebServersAsNewIISWebServerCollection()
- FriendSharedMachinenameAsString="localhost"
-
- PublicSubNew()
- start()
- EndSub
-
-
- PublicSubNew(ByValMachineName__1AsString)
- IfMachineName__1.ToString()<>""Then
- Machinename=MachineName__1
- EndIf
- start()
- EndSub
-
- PrivateSubstart()
- DimServiceAsNewDirectoryEntry("IIS://"+Machinename+"/W3SVC")
- DimServerAsDirectoryEntry
- DimRootAsDirectoryEntry=Nothing
- DimVirDirAsDirectoryEntry
- DimieAsIEnumerator=Service.Children.GetEnumerator()
- DimieRootAsIEnumerator
- DimitemAsIISWebServer
- Dimitem_virdirAsIISWebVirtualDir
- DimfindedAsBoolean=False
- Whileie.MoveNext()
- Server=DirectCast(ie.Current,DirectoryEntry)
- IfServer.SchemaClassName="IIsWebServer"Then
- item=NewIISWebServer()
- 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()
-
- WhileieRoot.MoveNext()
- Root=DirectCast(ieRoot.Current,DirectoryEntry)
- IfRoot.SchemaClassName="IIsWebVirtualDir"Then
- finded=True
- ExitWhile
- EndIf
- EndWhile
- IffindedThen
- item.Path=Root.Properties("path")(0).ToString()
- EndIf
-
- 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()
- WhileieRoot.MoveNext()
- VirDir=DirectCast(ieRoot.Current,DirectoryEntry)
- IfVirDir.SchemaClassName<>"IIsWebVirtualDir"AndAlsoVirDir.SchemaClassName<>"IIsWebDirectory"Then
- ContinueWhile
- EndIf
- item_virdir=NewIISWebVirtualDir(item.ServerComment)
- item_virdir.Name=VirDir.Name
- item_virdir.AccessRead=CBool(VirDir.Properties("AccessRead")(0))
- item_virdir.AccessScript=CBool(VirDir.Properties("AccessScript")(0))
- item_virdir.DefaultDoc=DirectCast(VirDir.Properties("DefaultDoc")(0),String)
- item_virdir.EnableDefaultDoc=CBool(VirDir.Properties("EnableDefaultDoc")(0))
- IfVirDir.SchemaClassName="IIsWebVirtualDir"Then
- item_virdir.Path=DirectCast(VirDir.Properties("Path")(0),String)
- ElseIfVirDir.SchemaClassName="IIsWebDirectory"Then
- item_virdir.Path=Root.Properties("Path")(0)+"/"+VirDir.Name
- EndIf
- item.WebVirtualDirs.Add_(item_virdir)
- EndWhile
- EndIf
- EndWhile
- EndSub
-
-
-
-
- PublicSharedSubCreateIISWebServer(ByValiisServerAsIISWebServer)
- IfiisServer.ServerComment.ToString()=""Then
- Throw(NewException("IISWebServer的ServerComment不能为空!"))
- EndIf
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry
- DimiAsInteger=0
- DimieAsIEnumerator=Service.Children.GetEnumerator()
-
- Whileie.MoveNext()
- Server=DirectCast(ie.Current,DirectoryEntry)
- IfServer.SchemaClassName="IIsWebServer"Then
- IfConvert.ToInt32(Server.Name)>iThen
- i=Convert.ToInt32(Server.Name)
- EndIf
- EndIf
- EndWhile
-
- i+=1
-
- Try
- iisServer.index=i
- Server=Service.Children.Add(i.ToString(),"IIsWebServer")
- Server.Properties("ServerComment")(0)=iisServer.ServerComment
- Server.Properties("Serverbindings").Add(":"+iisServer.Port+":")
- Server.Properties("AccessScript")(0)=iisServer.AccessScript
- Server.Properties("AccessRead")(0)=iisServer.AccessRead
- Server.Properties("EnableDirBrowsing")(0)=iisServer.EnableDirBrowsing
- Server.Properties("DefaultDoc")(0)=iisServer.DefaultDoc
- Server.Properties("EnableDefaultDoc")(0)=iisServer.EnableDefaultDoc
-
- DimrootAsDirectoryEntry=Server.Children.Add("Root","IIsWebVirtualDir")
- root.Properties("path")(0)=iisServer.Path
-
-
- Service.CommitChanges()
- Server.CommitChanges()
- root.CommitChanges()
- root.Invoke("AppCreate2",NewObject(0){2})
-
- CatchesAsException
- Throw(es)
- EndTry
- EndSub
-
-
-
- PublicSharedSubRemoveIISWebServer(ByValServerCommentAsString)
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry
- DimieAsIEnumerator=Service.Children.GetEnumerator()
-
- ServerComment=ServerComment.ToLower()
- Whileie.MoveNext()
- Server=DirectCast(ie.Current,DirectoryEntry)
- IfServer.SchemaClassName="IIsWebServer"Then
- IfServer.Properties("ServerComment")(0).ToString().ToLower()=ServerCommentThen
- Service.Children.Remove(Server)
- Service.CommitChanges()
- Return
- EndIf
- EndIf
- EndWhile
- EndSub
-
-
-
- PublicSharedSubRemoveIISWebServer(ByValindexAsInteger)
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- Try
- DimServerAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC/"+index)
- IfServerIsNotNothingThen
- Service.Children.Remove(Server)
- Service.CommitChanges()
- Else
- Throw(NewException("找不到此IISWebServer"))
- EndIf
- Catch
- Throw(NewException("找不到此IISWebServer"))
- EndTry
- EndSub
-
-
-
-
-
- PublicSharedFunctionExistsIISWebServer(ByValServerCommentAsString)AsBoolean
- ServerComment=ServerComment.Trim()
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry=Nothing
- DimieAsIEnumerator=Service.Children.GetEnumerator()
-
- DimcommentAsString
- Whileie.MoveNext()
- Server=DirectCast(ie.Current,DirectoryEntry)
- IfServer.SchemaClassName="IIsWebServer"Then
- comment=Server.Properties("ServerComment")(0).ToString().ToLower().Trim()
- Ifcomment=ServerComment.ToLower()Then
- ReturnTrue
- EndIf
- EndIf
- EndWhile
-
- ReturnFalse
- EndFunction
-
-
-
-
-
- FriendSharedFunctionreturnIISWebserver(ByValServerCommentAsString)AsDirectoryEntry
- ServerComment=ServerComment.Trim()
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry=Nothing
- DimieAsIEnumerator=Service.Children.GetEnumerator()
-
- DimcommentAsString
- Whileie.MoveNext()
- Server=DirectCast(ie.Current,DirectoryEntry)
- IfServer.SchemaClassName="IIsWebServer"Then
- comment=Server.Properties("ServerComment")(0).ToString().ToLower().Trim()
- Ifcomment=ServerComment.ToLower()Then
- ReturnServer
- EndIf
- EndIf
- EndWhile
-
- ReturnNothing
- EndFunction
-
-
-
-
-
- FriendSharedFunctionreturnIISWebserver(ByValindexAsInteger)AsDirectoryEntry
- DimServerAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC/"+index)
- Try
- DimieAsIEnumerator=Server.Children.GetEnumerator()
- ReturnServer
- Catch
- ReturnNothing
- EndTry
- EndFunction
-
- PrivateSharedFunctiongetRoot(ByValServerAsDirectoryEntry)AsDirectoryEntry
- ForEachchildAsDirectoryEntryInServer.Children
- DimnameAsString=child.Name.ToLower()
- Ifname="iiswebvirtualdir"OrElsename="root"Then
- Returnchild
- EndIf
- Next
- ReturnNothing
- EndFunction
-
-
-
-
- PublicSharedSubEditIISWebServer(ByValiisServerAsIISWebServer)
- IfiisServer.index=-1Then
- Throw(NewException("找不到给定的站点!"))
- EndIf
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry
-
- DimieAsIEnumerator=Service.Children.GetEnumerator()
-
- Whileie.MoveNext()
- Server=DirectCast(ie.Current,DirectoryEntry)
- IfServer.SchemaClassName="IIsWebServer"Then
- IfServer.Properties("Serverbindings")(0).ToString()=":"+iisServer.Port+":"Then
- Server.Invoke("stop",NewObject(-1){})
- EndIf
- EndIf
- EndWhile
-
- Server=returnIISWebserver(iisServer.index)
- IfServerIsNothingThen
- Throw(NewException("找不到给定的站点!"))
- EndIf
-
- Try
-
- Server.Properties("ServerComment")(0)=iisServer.ServerComment
- Server.Properties("Serverbindings")(0)=":"+iisServer.Port+":"
- Server.Properties("AccessScript")(0)=iisServer.AccessScript
- Server.Properties("AccessRead")(0)=iisServer.AccessRead
- Server.Properties("EnableDirBrowsing")(0)=iisServer.EnableDirBrowsing
- Server.Properties("DefaultDoc")(0)=iisServer.DefaultDoc
- Server.Properties("EnableDefaultDoc")(0)=iisServer.EnableDefaultDoc
-
- DimrootAsDirectoryEntry=getRoot(Server)
-
- Server.CommitChanges()
- IfrootIsNotNothingThen
- root.Properties("path")(0)=iisServer.Path
- root.CommitChanges()
- EndIf
-
- Server.Invoke("start",NewObject(-1){})
- CatchesAsException
- Throw(es)
- EndTry
- EndSub
-
-
-
-
- PublicSharedFunctionreturnIISServerComment()AsArrayList
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry
-
- DimlistAsNewArrayList()
- DimieAsIEnumerator=Service.Children.GetEnumerator()
-
- Whileie.MoveNext()
- Server=DirectCast(ie.Current,DirectoryEntry)
- IfServer.SchemaClassName="IIsWebServer"Then
- list.Add(Server.Properties("ServerComment")(0))
- EndIf
- EndWhile
-
- Returnlist
- EndFunction
-
-
-
-
-
-
- PublicSharedSubCreateIISWebVirtualDir(ByValiisVirAsIISWebVirtualDir,ByValdeleteIfExistAsBoolean)
- IfiisVir.ParentIsNothingThen
- Throw(NewException("IISWebVirtualDir没有所属的IISWebServer!"))
- EndIf
-
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry=returnIISWebserver(iisVir.Parent.index)
-
- IfServerIsNothingThen
- Throw(NewException("找不到给定的站点!"))
- EndIf
-
- Server=getRoot(Server)
- IfdeleteIfExistThen
- ForEachVirDirAsDirectoryEntryInServer.Children
- IfVirDir.Name.ToLower().Trim()=iisVir.Name.ToLower()Then
- Server.Children.Remove(VirDir)
- Server.CommitChanges()
- ExitFor
- EndIf
- Next
- EndIf
-
- Try
- DimvirAsDirectoryEntry
- vir=Server.Children.Add(iisVir.Name,"IIsWebVirtualDir")
- vir.Properties("Path")(0)=iisVir.Path
- vir.Properties("DefaultDoc")(0)=iisVir.DefaultDoc
- vir.Properties("EnableDefaultDoc")(0)=iisVir.EnableDefaultDoc
- vir.Properties("AccessScript")(0)=iisVir.AccessScript
- vir.Properties("AccessRead")(0)=iisVir.AccessRead
- vir.Invoke("AppCreate2",NewObject(0){2})
-
- Server.CommitChanges()
- vir.CommitChanges()
-
- CatchesAsException
- Throw(es)
- EndTry
-
- EndSub
-
-
-
-
-
- PublicSharedSubRemoveIISWebVirtualDir(ByValWebServerCommentAsString,ByValVirtualDirAsString)
- VirtualDir=VirtualDir.ToLower()
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry=returnIISWebserver(WebServerComment)
-
- IfServerIsNothingThen
- Throw(NewException("找不到给定的站点!"))
- EndIf
-
- Server=getRoot(Server)
- ForEachVirDirAsDirectoryEntryInServer.Children
- IfVirDir.Name.ToLower().Trim()=VirtualDirThen
- Server.Children.Remove(VirDir)
- Server.CommitChanges()
- Return
- EndIf
- Next
-
- Throw(NewException("找不到给定的虚拟目录!"))
- EndSub
-
-
- PublicSharedSubRemoveIISWebVirtualDir(ByValiisVirAsIISWebVirtualDir)
- DimServiceAsNewDirectoryEntry("IIS://"+IISManagement.Machinename+"/W3SVC")
- DimServerAsDirectoryEntry=returnIISWebserver(iisVir.Parent.index)
-
- IfServerIsNothingThen
- Throw(NewException("找不到给定的站点!"))
- EndIf
-
- Server=getRoot(Server)
- ForEachVirDirAsDirectoryEntryInServer.Children
- IfVirDir.Name.ToLower().Trim()=iisVir.Name.ToLower()Then
- Server.Children.Remove(VirDir)
- Server.CommitChanges()
- Return
- EndIf
- Next
-
- Throw(NewException("找不到给定的虚拟目录!"))
- EndSub
- EndClass
-
- EndNamespace
PS:参考版为C#,来源已无法考证。
原文链接:https://www.f2er.com/vb/264002.html