65.9K
CodeProject 正在变化。 阅读更多。
Home

IIsManager

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (9投票s)

2008年8月20日

Ms-PL
viewsIcon

39053

downloadIcon

231

使用 .NET 管理 IIS 6

引言

这个类库将允许您使用 .NET 创建和修改 IIS6 中的网站、应用程序池、虚拟目录等。

背景

如果您曾经需要自动化创建 Web 应用程序、应用程序池、虚拟目录等,您可能会发现我制作的这个 IIsManager 类库非常有用。使用 System.DirectoryServices 命名空间来管理 IIS 6 并不是很直接,所以我决定将 IIS 6 封装到这些类中。

  • IIsService
  • IIsApplicationPool
  • IIsSite
  • IIsDirectory
  • IIsFile
  • IIsVirtualDirectory

实例化 IIsService 类后,您可以使用它来遍历站点(如果您想使用 LINQ),添加或删除它们,以及设置它们的属性。

Using the Code

以下示例将创建一个网站,将其设置为使用 ASP.NET 2.0,创建一个应用程序池并将其设置为使用它,更改目录上的访问权限以及创建一个包含 Web 应用程序的虚拟目录

' Instantiate an IIsService which represents the W3SVC service on the localhost.
Using IIsSvc = New IIsService

    ' Creates an IIsSite.
    Using testsite = IIsSvc.AddSite("TestSite", "c:\inetpub\testsite", "test.site.nl")

        ' Sets AccessPermissions to allow reading and executing scripts (.aspx).
        testsite.AccessPermissions = AccessPermissionFlags.Read + _
					AccessPermissionFlags.Script

        ' Sets the website to use ASP.NET 2.0
        testsite.ASPNETVersion = ASPNETVersions.v2_0_50727

        ' Create an IIsApplicationPool.
        Dim testpool = IIsSvc.AddAppPool("testpool")

        ' Set the site to use the new application pool.
        testsite.ApplicationPoolId = testpool.Id

        ' Creates an IIsDirectory and changes the AccessPermissions 
        ' to allow writing to it.
        ' Note that the physical directory already has to exist 
        ' since we're just creating metabase information.
        Using images = testsite.AddDirectory("images")
            images.AccessPermissions = AccessPermissionFlags.Read + _
					AccessPermissionFlags.Write
        End Using

        ' Creates an IIsVirtualDirectory and creates a Web application in it.
        Using newapp = testsite.AddVirtualDirectory("newapp", "c\inetpub\newapp")
            newapp.CreateApplication()
            newapp.AccessPermissions = AccessPermissionFlags.Read + _
					AccessPermissionFlags.Script
        End Using

        ' Starts the website. It is stopped at creation by default.
        testsite.StartSite()

    End Using

    ' Because the classes implement IDisposable, 
    ' the Dispose method is automatically called
    ' at 'End Using'.
End Using

历史

  • 2008 年 8 月 20 日:首次发布
© . All rights reserved.