IIS API - 创建虚拟目录?

时间:2008-11-05 17:50:34

标签: .net asp.net iis api virtual-directory

只需查找相关文档。一个例子没有必要,但我们将不胜感激。

我们有一种情况,我们必须手动创建100个虚拟目录,似乎自动化这将是一个很好的方法,使现在的过程更有效。

也许明年我们可以重新设计服务器环境以允许更加理智的东西,例如URL重写(遗憾的是,这在Web应用程序的当前循环中似乎不可行)。继承垃圾代码不是很好吗?

~William Riley-Land

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 - Using IIS Programmatic Administration

答案 2 :(得分: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();
            //}
        }
    }
}

答案 3 :(得分:1)

WIX安装程序工具创建了一种管理方法 - 您可以将它们定义为安装构建说明的一部分。它做了一些工作来完成所有项目的配置,但在那之后维护应该是轻而易举的......以下是关于创建虚拟目录条目的教程的摘录......

6.3网站目录

  

WiX工具集具有其他库,允许安装程序执行其他任务,例如在IIS中创建Web目录。要使用这些扩展,您所要做的就是链接相应的WiX库。链接器将自动将必要的帮助程序DLL包含在安装包中。

     

首先,我们必须使用属于它的文件创建网站:

     

&lt; Directory Id ='TARGETDIR'Name ='SourceDir'&gt;
     &lt; Directory Id ='ProgramFilesFolder'Name ='PFiles'&gt;
      &lt; Directory Id ='InstallDir'Name ='Acme'&gt;
        &lt; Component Id ='default.phpComponent'Guid ='YOURGUID-5314-4689-83CA-9DB5C04D5742'&gt;

          &lt; File Id ='default.htmFile'Name ='default.htm'LongName ='default.htm'KeyPath ='yes'             DiskId ='1'Source ='default.htm'/&gt;
        &LT; /成分&gt;
      &LT; /目录&gt;
    &LT; /目录&gt;

     

下一步是创建虚拟目录:

     

&lt; Component Id ='TestWebVirtualDirComponent'Guid ='YOURGUID-6304-410E-A808-E3585379EADB'&gt;
      &lt; WebVirtualDir Id ='TestWebVirtualDir'Alias ='Test'Directory ='InstallDir'WebSite ='DefaultWebSite'&gt;

        &lt; WebApplication Id ='TestWebApplication'Name ='Test'/&gt;
      &LT; / WebVirtualDir&GT;
    &LT; /成分&gt;
  &LT; /目录&gt;

     

最后,创建一个引用该网站的条目:

     

&lt; WebSite Id ='DefaultWebSite'Description ='默认网站'&gt;
    &lt; WebAddress Id ='AllUnassigned'Port ='80'/&gt;
    &LT; /网站和GT;