IIS API - 创建虚拟目录?

6

我只是在寻找相关的文件,不需要示例,但如果有的话会很感激。

我们的情况是需要手动创建数百个虚拟目录,因此自动化这个过程似乎是提高效率的好方法。

也许明年我们可以重新设计服务器环境,采用更合理的方式,例如URL重写(不幸的是,在当前Web应用程序的生命周期内似乎并不可行)。继承烂代码真是太好了。

~ William Riley-Land

4个回答

6

使用 VBScript 更容易实现。

' This code creates a virtual directory in the default Web Site
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strComputer = "rallen-w2k3"
strVdirName = "<VdirName>"  'e.g. employees
strVdirPath = "<Path>"      'e.g. D:\resumes
' ------ END CONFIGURATION ---------
set objIIS = GetObject("IIS://" & strComputer & "/W3SVC/1")
set objWebSite = objIIS.GetObject("IISWebVirtualDir","Root")
set objVdir = objWebSite.Create("IISWebVirtualDir",strVdirName)
objVdir.AccessRead = True
objVdir.Path = strVdirPath
objVdir.SetInfo
WScript.Echo "Successfully created virtual directory: " & objVdir.Name

4
显然,您也可以通过PowerShell脚本来完成这个任务:
$objIIS = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root")
$children = $objIIS.psbase.children
$vDir = $children.add("NewFolder",$objIIS.psbase.SchemaClassName)
$vDir.psbase.CommitChanges()
$vDir.Path = "C:\Documents and Settings\blah\Desktop\new"
$vDir.defaultdoc = "Default.htm"
$vDir.psbase.CommitChanges()

以下是文档:MSDN - 使用IIS编程管理


2

未经测试(来自我以前承包商编写的旧代码库)

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.IO;

namespace Common.DirectoryServices
{
    public class IISManager
    {

        private string _webSiteID;

        public string WebSiteID
        {
            get { return _webSiteID; }
            set { _webSiteID = value; }
        }

        private string _strServerName;
        public string ServerName
        {
            get
            {
                return _strServerName;
            }
            set
            {
                _strServerName = value;
            }
        }

        private string _strVDirName;
        public string VDirName
        {
            get
            {
                return _strVDirName;
            }
            set
            {
                _strVDirName = value;
            }
        }

        private string _strPhysicalPath;
        public string PhysicalPath
        {
            get
            {
                return _strPhysicalPath;
            }
            set
            {
                _strPhysicalPath = value;
            }
        }

        private VDirectoryType _directoryType;
        public VDirectoryType DirectoryType
        {
            get
            {
                return _directoryType;
            }
            set
            {
                _directoryType = value;
            }
        }

        public enum VDirectoryType
        {
            FTP_DIR, WEB_IIS_DIR
        };

        public string CreateVDir()
        {
            System.DirectoryServices.DirectoryEntry oDE;
            System.DirectoryServices.DirectoryEntries oDC;
            System.DirectoryServices.DirectoryEntry oVirDir;
            //try
           // {
                //check whether to create FTP or Web IIS Virtual Directory
                if (this.DirectoryType == VDirectoryType.WEB_IIS_DIR)
                {
                    oDE = new DirectoryEntry("IIS://" +
                          this._strServerName + "/W3SVC/" + _webSiteID + "/Root");
                }
                else
                {
                    oDE = new DirectoryEntry("IIS://" +
                          this._strServerName + "/MSFTPSVC/1/Root");
                }

                //Get Default Web Site
                oDC = oDE.Children;

                //Add row
                oVirDir = oDC.Add(this._strVDirName,
                          oDE.SchemaClassName.ToString());

                //Commit changes for Schema class File
                oVirDir.CommitChanges();

                //Create physical path if it does not exists
                if (!Directory.Exists(this._strPhysicalPath))
                {
                    Directory.CreateDirectory(this._strPhysicalPath);
                }

                //Set virtual directory to physical path
                oVirDir.Properties["Path"].Value = this._strPhysicalPath;

                //Set read access
                oVirDir.Properties["AccessRead"][0] = true;

                //Create Application for IIS Application (as for ASP.NET)
                if (this.DirectoryType == VDirectoryType.WEB_IIS_DIR)
                {
                    oVirDir.Invoke("AppCreate", true);
                    oVirDir.Properties["AppFriendlyName"][0] = this._strVDirName;
                }

                //Save all the changes
                oVirDir.CommitChanges();

                return null;

           // }
            //catch (Exception exc)
            //{
             //   return exc.Message.ToString();
            //}
        }
    }
}

1
WIX 安装程序工具可以创建一种管理方式 - 您可以在安装构建说明中定义它们。配置项目需要一些工作,但之后的维护就会非常轻松...以下是有关创建虚拟目录条目的教程摘录...

6.3 Web Directory

WiX 工具集有额外的库,允许安装程序执行其他任务,例如在 IIS 中创建 Web 目录。要使用这些扩展,您只需链接到相应的 WiX 库即可。链接器将自动将所需的辅助 DLL 包含在安装程序包中。
首先,我们必须使用相应的文件创建网站:                                                                        下一步是创建虚拟目录:                    最后,创建一个条目来引用该网站:     

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接