如何通过代码来创建IIS虚拟目录

解决方案 »

  1.   

    http://www.cnblogs.com/newsea/archive/2008/08/28/1278766.html
      

  2.   


    using System.IO;
    using System.DirectoryServices;// .....    /// <summary>
        /// 创建iis虚拟目录
        /// </summary>
        /// <param name="physicalPath">虚拟目录所指向的物理路径</param>
        /// <param name="virtualDirName">虚拟目录名称</param>
        private void CreateDirectory(string physicalPath, string virtualDirName)
        {
            if (!DirectoryEntry.Exists("IIS://localhost/W3SVC/1/ROOT" + "\\" + virtualDirName))
            {
                try
                {
                    DirectoryEntry root = new DirectoryEntry(IISWebSiteRoot);
                    DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName);                tbEntry.Properties["Path"][0] = physicalPath;    //设置虚拟目录指向的物理路径
                    tbEntry.Invoke("AppCreate", true);                tbEntry.Properties["AccessRead"][0] = true;     //设置读取权限                //  tbEntry.Properties["AccessRead"][0] = false;
                    tbEntry.Properties["ContentIndexed"][0] = true;
                    tbEntry.Properties["DefaultDoc"][0] = "Index.aspx,Default.aspx";   //设置默认文档,多值情况下中间用逗号分割
                    tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;        //应用程序名称
                    tbEntry.Properties["AccessScript"][0] = true;                     //执行权限
                    tbEntry.Properties["DontLog"][0] = true;
                    tbEntry.Properties["AuthFlags"][0] = 0;    //设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
                    tbEntry.Properties["AuthFlags"][0] = 1;
                    tbEntry.CommitChanges();
                }
                catch (Exception ex)
                {
                    Respone.write("<script>alert('" + ex.Message + '');<script>');
                }
            }       
        }
      

  3.   

    转自:http://www.cnblogs.com/libiyang/archive/2009/01/04/294455.html
    如何使用DirectoryEntry来实现IIS虚拟目录的管理1、创建虚拟目录:
    创建虚拟目录的经过以下步骤:
    1)获取该虚拟目录的上级目录的DirectoryEntry对象rootEntry;
    2)通过rootEntry的DirectoryEntry::Childrens.Add来添加该虚拟目录;
    // 创建虚拟目录
                    DirectoryEntry entry = rootEntry.Children.Add(this.m_strAlias, "IIsWebVirtualDir");
    3)更新该虚拟目录的属性,如更新身份验证模式,访问权限和所对应的物理目录等。需要注意的是,使用DirectoryEntry来创建虚拟目录,只能在该虚拟目录建立了以后,才能设置物理目录等属性。创建虚拟目录的代码如下:
     1 /// <summary>
     2         /// 创建iis虚拟目录
     3         /// </summary>
     4         /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>
     5         public override void CreateDirectory()
     6         {
     7             // 已不覆盖的方式创建虚拟目录,当虚拟目录存在时抛出异常
     8             this.CreateDirectory(false);
     9         }
    10 
    11         /// <summary>
    12         /// 创建iis虚拟目录
    13         /// </summary>
    14         /// <param name="bReplace">是否覆盖掉原有的虚拟目录</param>
    15         /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>
    16         public override void CreateDirectory(bool bReplace)
    17         {
    18             // 判断目录是否存在
    19             if (this.Exist())
    20             {
    21                 if (bReplace)
    22                 {
    23                     // 若允许覆盖则先删除原有的虚拟目录
    24                     this.DeleteDirectory();
    25                 }
    26                 else
    27                 {
    28                     // 若不允许覆盖直接抛出目录已存在的异常
    29                     DirectoryException.Throw("directory already exist");
    30                 }
    31             }
    32 
    33             try
    34             {
    35                 // 获取上级目录的DirectoryEntry对象
    36                 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);
    37                 // 创建虚拟目录
    38                 DirectoryEntry entry = rootEntry.Children.Add(this.m_strAlias, "IIsWebVirtualDir");
    39                 entry.Invoke("AppCreate", true);
    40                 entry.CommitChanges();
    41                 rootEntry.CommitChanges();
    42 
    43                 // 更新虚拟目录属性
    44                 SystemDirectory.UpdateEntry(entry, this.Property);
    45             }
    46             catch (System.Exception ex)
    47             {
    48                 DirectoryException.Throw(ex.Message);
    49             }
    50         }
    DirectoryEntry 的commitChanges方法用于提交DirectoryEntry的操作。line41中的提交了以后,该虚拟目录才能建立,然后在进行更新虚拟目录的属性的操作。若没有commitChanges就进行更新操作,会抛出找不到虚拟目录的异常,有兴趣的朋友可以试试。2、删除虚拟目录:
    删除虚拟目录比较简单,只需调用虚拟目录的父节点的DirectoryEntry对象的Delete操作就可以了,需要注意的是Delete对象需要两个参数:子节点的Alias和子节点的类型(虚拟目录节点的类型为IIsWebVirtualDir)。
     1 /// <summary>
     2         /// 删除iis虚拟目录
     3         /// </summary>
     4         /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>
     5         public override void DeleteDirectory()
     6         {
     7             // 判断目录是否存在
     8             if (! this.Exist())
     9             {
    10                 // 若待删除的虚拟目录不存在,则抛出异常
    11                 DirectoryException.Throw("directory does not exist");
    12             }
    13 
    14             try
    15             {
    16                 // 获取上级目录的DirectoryEntry对象
    17                 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);
    18                 // 删除参数
    19                 object[] objParams = new object[2];;
    20                 objParams[0] = "IIsWebVirtualDir";
    21                 objParams[1] = this.m_strAlias;
    22                 // 删除虚拟目录
    23                 rootEntry.Invoke("Delete", objParams);
    24                 rootEntry.CommitChanges();
    25             }
    26             catch (System.Exception ex)
    27             {
    28                 DirectoryException.Throw(ex.Message);
    29             }
    30             
    31         }3、判断虚拟目录是否存在:
    通过调用父节点的DirectoryEntry对象的DirectoryEntry::Children.Find来查找子结点是否存在,但非常奇怪的当子节点不存在的时候会抛出异常,我更期望的是返回子节点的null引用。
     1 /// <summary>
     2         /// 判断iis虚拟目录是否存在
     3         /// </summary>
     4         /// <returns>目录是否存在</returns>
     5         public override bool Exist()
     6         {
     7             bool bExist = false;
     8             
     9             try
    10             {
    11                 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);
    12                 DirectoryEntry entry = rootEntry.Children.Find(this.Alias, "IIsWebVirtualDir");
    13                 bExist = (entry != null);
    14             }
    15             catch{}
    16 
    17             return bExist;
    18         }
      

  4.   

    上面的很详细了吧
    不知道你ADSI听说没