一个关于IIS的类

来源:互联网 发布:python脚本编写实例 编辑:程序博客网 时间:2024/05/22 11:18

Imports System

Imports System.DirectoryServices

Imports System.IO

Public Class IISManager 

    Public Shared Function CreateWebSite(ByVal webSiteName As String, ByVal pathToRoot As String) As WebSite

        Dim root As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC") 

        ' Find unused ID value for new web site

        Dim siteID As Integer = 1

        Dim e As DirectoryEntry

        For Each e In root.Children

            If e.SchemaClassName = "IIsWebServer" Then

                Dim ID As Integer = Convert.ToInt32(e.Name)

                If ID >= siteID Then

                    siteID = ID + 1

                End If

            End If

        Next

        ' Create the root directory and welcome html file

        CreatePhysicalDirectory(pathToRoot, webSiteName) 

        ' Create web site

        Dim site As New WebSite(CType(root.Invoke("Create", "IIsWebServer", siteID), DirectoryEntry))

        site.ServerComment = webSiteName

        'site.KeyType = "IIsWebServer"

        site.ServerBindings = ":80:" + webSiteName

        site.MaxBandwidth = "1048576"

        site.ServerState = 2

        site.LogFlags.LogExtFileFlags = 340871        

        site.FrontPageWeb = 1

        'site.DefaultDoc = "Default.aspx"

        'site.SecureBindings = ":443:" + webSiteName

        site.ServerAutoStart = 1

        site.ServerSize = 1 

        site.SetInfo()

        ' Create application virtual directory

        Dim siteVDir As DirectoryEntry = site.Children.Add("Root", "IISWebVirtualDir")

        siteVDir.Properties("AppIsolated")(0) = 2

        siteVDir.Properties("Path")(0) = pathToRoot

        siteVDir.Properties("AccessFlags")(0) = 513

        siteVDir.Properties("FrontPageWeb")(0) = 1

        siteVDir.Properties("AppRoot")(0) = "/LM/W3SVC/" + siteID.ToString() + "/Root"

        'siteVDir.Properties("AppFriendlyName")(0) = "Root"

        siteVDir.CommitChanges()

        site.CommitChanges() 

        siteVDir.Close()

        site.Close()

        ' Create temp virtual directory on Default Web Site

        Dim deRoot As New DirectoryEntry("IIS://localhost/W3SVC/1/Root")

        deRoot.RefreshCache() 

        Dim deNewVDir As DirectoryEntry = deRoot.Children.Add(webSiteName.Replace("."c, "-"c), "IIsWebVirtualDir")

        deNewVDir.Properties("Path")(0) = pathToRoot

        deNewVDir.Properties("AccessFlags")(0) = 513

        deNewVDir.Properties("FrontPageWeb")(0) = 1

        deNewVDir.CommitChanges()

        deRoot.CommitChanges()

        'Create a Application

        deNewVDir.Invoke("AppCreate", True) 

        'Save Changes

        deNewVDir.CommitChanges()

        deRoot.CommitChanges()

        deNewVDir.Close()

        deRoot.Close() 

        Return site

    End Function 

    Private Shared Sub CreatePhysicalDirectory(ByVal pathToRoot As String, ByVal webSiteName As String)

        If (Directory.Exists(pathToRoot)) Then

            Throw New Exception("Error creating new website: Customer root directory already exists at " + pathToRoot)

        Else

            Directory.CreateDirectory(pathToRoot)

            Dim indexFile As New StreamWriter(pathToRoot + "/index.htm", False, System.Text.Encoding.ASCII)

            indexFile.Write("



Welcome to  " + webSiteName + "

")

            indexFile.Close()

        End If

    End Sub 

End Class

Public Class WebSite

    Public Sub New(ByVal ObjDirectoryEntry As DirectoryEntry)

        site = ObjDirectoryEntry

        logFileFlags = New LogExtFileFlags(ObjDirectoryEntry)

    End Sub 

    Private site As DirectoryEntry

    Private logFileFlags As LogExtFileFlags

    Public ReadOnly Property LogFlags() As LogExtFileFlags

        Get

            Return logFileFlags

        End Get

    End Property 

    Public Property ServerComment() As String

        Get

            Return CType(site.Properties("ServerComment")(0), String)

        End Get

        Set(ByVal Value As String)

            site.Invoke("Put", "ServerComment", Value)

        End Set

    End Property

    Public Property KeyType() As String

        Get

            Return CType(site.Properties("KeyType")(0), String)

        End Get

        Set(ByVal Value As String)

            site.Invoke("Put", "KeyType", Value)

        End Set

    End Property 

    Public Property ServerBindings() As String

        Get

            Return CType(site.Properties("ServerBindings")(0), String)

        End Get

        Set(ByVal Value As String)

            site.Invoke("Put", "ServerBindings", Value)

        End Set

    End Property

    Public Property DefaultDoc() As String

        Get

            Return CType(site.Properties("DefaultDoc")(0), String)

        End Get

        Set(ByVal Value As String)

            site.Invoke("Put", "DefaultDoc", Value)

        End Set

    End Property 

    Public Property MaxBandwidth() As String

        Get

            Return CType(site.Properties("MaxBandwidth")(0), String)

        End Get

        Set(ByVal Value As String)

            site.Invoke("Put", "MaxBandwidth", Value)

        End Set

    End Property

    Public Property ServerState() As Int32

        Get

            Return CType(site.Properties("ServerState")(0), Int32)

        End Get

        Set(ByVal Value As Int32)

            site.Invoke("Put", "ServerState", Value)

        End Set

    End Property 

    Public Property FrontPageWeb() As Int32

        Get

            Return CType(site.Properties("FrontPageWeb")(0), Int32)

        End Get

        Set(ByVal Value As Int32)

            site.Invoke("Put", "FrontPageWeb", Value)

        End Set

    End Property

    Public Property SecureBindings() As String

        Get

            Return CType(site.Properties("SecureBindings")(0), String)

        End Get

        Set(ByVal Value As String)

            site.Invoke("Put", "SecureBindings", Value)

        End Set

    End Property 

    Public Property ServerAutoStart() As Int32

        Get

            Return CType(site.Properties("ServerAutoStart")(0), Int32)

        End Get

        Set(ByVal Value As Int32)

            site.Invoke("Put", "ServerAutoStart", Value)

        End Set

    End Property

    Public Property ServerSize() As Int32

        Get

            Return CType(site.Properties("ServerSize")(0), Int32)

        End Get

        Set(ByVal Value As Int32)

            site.Invoke("Put", "ServerSize", Value)

        End Set

    End Property 

    Public ReadOnly Property Children() As DirectoryEntries

        Get

            Return site.Children

        End Get

    End Property

    Public ReadOnly Property Name() As String

        Get

            Return site.Name

        End Get

    End Property 

    Public Sub CommitChanges()

        site.CommitChanges()

    End Sub

    Public Sub SetInfo()

        site.Invoke("SetInfo")

    End Sub 

    Public Sub Close()

        site.Close()

    End Sub 

End Class

Public Class LogExtFileFlags

    Public Sub New(ByVal ObjDirectoryEntry As DirectoryEntry)

        site = ObjDirectoryEntry

    End Sub 

    Private site As DirectoryEntry

    Public Property LogExtFileFlags() As Int32

        Get

            Return CType(site.Properties("LogExtFileFlags")(0), Int32)

        End Get

        Set(ByVal Value As Int32)

            site.Invoke("Put", "LogExtFileFlags", Value)

        End Set

    End Property 

    'Public Property LogExtFileBytesRecv() As Boolean

    '    Get

    '        Return CBool(site.Properties("LogExtFileBytesRecv")(0))

    '    End Get

    '    Set(ByVal Value As Boolean)

    '        Dim showInLog As Int32 = 0

    '        If Value Then

    '            showInLog = 1

    '        End If

    '        site.Invoke("Put", "LogExtFileBytesRecv", showInLog)

    '    End Set

    'End Property

 End Class