代码之家  ›  专栏  ›  技术社区  ›  wprl

IIS API-创建虚拟目录?

  •  6
  • wprl  · 技术社区  · 17 年前

    只需查找相关文档。无需举例说明,但如有必要,将不胜感激。

    我们有一种情况,我们必须手动创建100个虚拟目录,现在看来,自动化这将是一种提高流程效率的好方法。

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

    4 回复  |  直到 17 年前
        1
  •  6
  •   Gulzar Nazim    17 年前

    这样做比较容易 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
    
        2
  •  4
  •   Turnkey    17 年前

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

        3
  •  2
  •   John Sheehan    17 年前

    未测试(来自旧代码库,由矿山前承包商编写)

    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();
                //}
            }
        }
    }
    
        4
  •  1
  •   silverbugg    17 年前

    6.3网页目录

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

    <目录Id='TARGETDIR'Name='SourceDir'>
    <目录Id='ProgramFilesFolder'Name='PFiles'>


    <文件Id='default.htmFile'Name='default.htm'LongName='default.htm'KeyPath='yes'
    </组件>
    </目录>
    </目录>

    下一步是创建虚拟目录:



    <WebApplication Id='TestWebApplication'Name='Test'/>
    </WebVirtualDir>
    </组件>
    </目录>

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

    <网站Id='DefaultWebSite'说明='DefaultWebSite'>
    <网络地址Id='AllUnassigned'端口='80'/>

    推荐文章