IIS API - 创建虚拟目录?

发布于 2024-07-08 14:10:25 字数 207 浏览 8 评论 0原文

只是寻找相关文档。 示例不是必需的,但我们将不胜感激。

我们遇到的情况是,我们必须手动创建数百个虚拟目录,而自动化似乎是目前提高流程效率的好方法。

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

〜威廉·赖利·兰德

Just looking for the relevant documentation. An example is not necessary, but would be appreciated.

We have a situation where we are having to create 100s of virtual directories manually, and it seems like automating this would be a good way to make the process more efficient for now.

Perhaps next year we can rework the server environment to allow something more sane, such as URL rewriting (unfortunately this does not seem feasible in the current cycle of the web application). Isn't it great to inherit crap code?

~ William Riley-Land

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你好,陌生人 2024-07-15 14:10:25

使用 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

It is easier to do it with vbscript too..

' 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
呆° 2024-07-15 14:10:25

显然,您也可以通过 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 编程管理

Evidently you can also do this via PowerShell scripting:

$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()

Here is the documentation: MSDN - Using IIS Programmatic Administration

从﹋此江山别 2024-07-15 14:10:25

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

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();
            //}
        }
    }
}

NOT TESTED (from an old code base and written by a former contractor of mine)

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();
            //}
        }
    }
}
难理解 2024-07-15 14:10:25

WIX 安装程序工具创建了一种管理此操作的方法 - 您可以将它们定义为安装构建指令的一部分。 配置所有项目需要一些工作,但之后维护应该变得轻而易举...这是关于创建虚拟目录条目的教程的摘录...

6.3 Web 目录

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

首先,我们必须创建网站及其所属文件:

<目录 ID='TARGETDIR' Name='SourceDir'>
  <目录 ID='ProgramFilesFolder' Name='PFiles'>
    <目录 Id='InstallDir' Name='Acme'>
     <组件 Id='default.phpComponent' Guid='YOURGUID-5314-4689-83CA-9DB5C04D5742'>
      <文件 Id='default.htmFile' Name='default.htm' LongName='default.htm' KeyPath='yes'
DiskId='1' Source='default.htm' />
      
    
  

下一步是创建虚拟目录:

    <组件 Id='TestWebVirtualDirComponent' Guid='YOURGUID-6304-410E-A808-E3585379EADB'>
       
       
       

      
    

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

  <网站 Id='DefaultWebSite' Description='默认网站'>
   
  

The WIX installer tool creates a way to manage this - you would define them as a part of the installation build instructions. It make some work to get all of the project configured but after that maintenance should be a breeze... Here is an excerpt from the Tutorial on creating the Virtual Directory entries...

6.3 Web Directory

The WiX toolset has additional libraries that allow the installer to perform additional tasks like creating a web directory in IIS. To use these extensions, all you have to do is to link against the appropriate WiX library. The linker will include the necessary helper DLLs into the installation package automatically.

First, we have to create the web site with the files belonging to it:

<Directory Id='TARGETDIR' Name='SourceDir'>
  <Directory Id='ProgramFilesFolder' Name='PFiles'>
    <Directory Id='InstallDir' Name='Acme'>
      <Component Id='default.phpComponent' Guid='YOURGUID-5314-4689-83CA-9DB5C04D5742'>
        <File Id='default.htmFile' Name='default.htm' LongName='default.htm' KeyPath='yes'
DiskId='1' Source='default.htm' />
      </Component>
    </Directory>
  </Directory>

The next step is to create the virtual directory:

      <Component Id='TestWebVirtualDirComponent' Guid='YOURGUID-6304-410E-A808-E3585379EADB'>
        <WebVirtualDir Id='TestWebVirtualDir' Alias='Test' Directory='InstallDir' WebSite='DefaultWebSite'>
          <WebApplication Id='TestWebApplication' Name='Test' />
        </WebVirtualDir>
      </Component>
    </Directory>

Finally, create an entry to reference the web site:

  <WebSite Id='DefaultWebSite' Description='Default Web Site'>
   <WebAddress Id='AllUnassigned' Port='80' />
  </WebSite>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文