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

IIS 实用程序创建虚拟目录 - 高级

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.28/5 (7投票s)

2007年9月12日

CPOL
viewsIcon

56379

downloadIcon

322

此工具可用于一键创建带有额外设置的新虚拟目录!

Screenshot - IISUtility.jpg

引言

这是一个简单的 IIS 工具,用于使用预定义的设置创建新的虚拟目录。 我在互联网上找到了一段创建虚拟目录的示例代码 (原始文章在此)。 我只是在上面添加了一些功能……
因此,功劳归于原始作者。

背景

在大型项目中,通常需要创建和配置新的虚拟目录。 我试图创建这个工具,以方便配置管理团队的工作。

Using the Code

代码已附上。 现在轮到你通过添加更多功能来增强这个工具了!

Dim IISSchema As New System.DirectoryServices.DirectoryEntry_
    ("IIS:///Schema/AppIsolated")
            Dim CanCreate As Boolean = _
                Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
            IISSchema.Dispose()
            Dim AppName As String = txtVirtDirName.Text, path As String = txtPath.Text
            If CanCreate Then
                Dim PathCreated As Boolean

                Dim IISAdmin As New System.DirectoryServices.DirectoryEntry_
                        ("IIS:///W3SVC/1/Root")
                'make sure folder exists
                If Not System.IO.Directory.Exists(path) Then
                    System.IO.Directory.CreateDirectory(path)
                    PathCreated = True
                End If

                Dim VDir As System.DirectoryServices.DirectoryEntry = Nothing
                Dim bAlreadyThere As Boolean = False
                'If the virtual directory already exists then delete it
                For Each VD As System.DirectoryServices.DirectoryEntry In _
                        IISAdmin.Children()
                    If VD.Name = AppName Then
                        IISAdmin.Invoke("Delete", New String() _
                        {VD.SchemaClassName, AppName})
                        IISAdmin.CommitChanges()
                        'MsgBox("Virtual directory already exists..!.", _
                            MsgBoxStyle.Information, "Create Virtual Directory")
                        'Exit Sub
                        VDir = VD
                        bAlreadyThere = False
                        Exit For
                    End If
                Next VD

                'Create and setup new virtual directory
                If (Not bAlreadyThere) Then
                    VDir = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir")
                End If
                VDir.Properties("Path").Item(0) = path
                VDir.Properties("AppFriendlyName")(0) = AppName
                VDir.Properties("EnableDirBrowsing").Item(0) = True
                VDir.Properties("AccessRead").Item(0) = True
                VDir.Properties("AccessExecute").Item(0) = True
                VDir.Properties("AccessWrite").Item(0) = False
                VDir.Properties("AccessScript").Item(0) = True
                VDir.Properties("AuthNTLM").Item(0) = chkWinAuthenticate.Checked _
                    'True 'Integreted Windows(Authontocation)
                VDir.Properties("AuthAnonymous").Item(0) = chkAnonymous.Checked
                If (chkAnonymous.Checked) Then
                    VDir.Properties("AnonymousUserName").Item(0) = txtUser.Text
                    VDir.Properties("AnonymousUserPass").Item(0) = txtPassword.Text
                    VDir.Properties("AnonymousPasswordSync").Item(0) = False
                End If
                VDir.Properties("EnableDefaultDoc").Item(0) = chkDefaultDocs.Checked
                VDir.Properties("DefaultDoc").Item(0) = txtDefaultDocuments.Text
                VDir.Properties("AspEnableParentPaths").Item(0) = True

                VDir.CommitChanges()

                If Not bAlreadyThere Then VDir.Invoke("AppCreate2", 2)
                If (chkASPNET2.Checked) Then
                    Dim prcProcess As System.Diagnostics.Process
                    prcProcess = System.Diagnostics.Process.Start_
                    (System.Environment.GetEnvironmentVariable("windir") + _
                        "\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis", _
                        " -s /W3SVC/1/Root/" + txtVirtDirName.Text)
                    prcProcess.WaitForExit()
                End If
                MessageBox.Show("Done", "Hurray...", MessageBoxButtons.OK, _
                    MessageBoxIcon.Information)

            End If

特点

  1. 创建新的虚拟目录,覆盖现有的目录
  2. 配置 ASP.NET 2.0 或 1.1 Framework
  3. 配置匿名访问下的自定义用户
  4. 配置默认页面
  5. 立即测试

增强功能 -- 你的任务!

  1. 我没有 IIS 7。 在 IIS 7 上测试,并告诉我结果
  2. 在 IIS 6 中配置适当的应用程序池

注意:这只是原始代码,用于了解如何管理虚拟目录。 您需要根据需要修改代码。

© . All rights reserved.