遍历IIS站点

2011-1-27 14:27:58 - tanlixiao

.net提供DirectoryEntry类可读取和操作IIS的站点信息,下面贴出部分代码,代码如下:

string entPath = String.Format("IIS://{0}/w3svc", HostName);
DirectoryEntry ent = GetDirectoryEntry(entPath);
foreach (DirectoryEntry child in ent.Children)
            {
                if (child.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
                {
                    if (child.Properties["ServerBindings"].Value != null)
                    {
                        #region 取得主机头
                        object objectArr = child.Properties["ServerBindings"].Value;
                        List<string> serverBindinglist = new List<string>();
                        if (IsArray(objectArr))
                        {
                            object[] objectToArr = (object[])objectArr;
                            foreach (object item in objectToArr)
                            {
                                serverBindinglist.Add(GetWebHostName(item.ToString()));
                            }
                        }
                        else
                        {
                            serverBindinglist.Add(GetWebHostName(child.Properties["ServerBindings"].Value.ToString()));
                        }
                        #endregion

                        foreach (string serveritem in serverBindinglist)
                        {
                            IList<VirtualDirectory> selectVir = IsContainsIn(virConfigList, serveritem);//查找虚拟目录中是否有该主机头
                            if (selectVir != null && selectVir.Count > 0)//在主机头列表中找到
                            {
                                DirectoryEntry virEntry = new DirectoryEntry(child.Path "/ROOT");//该站点的根目录
                                VirtualDirectories virtualDirectories = GetVirDirs(virEntry.Children);//站点的所有虚拟目录
                                foreach (VirtualDirectory subItem in selectVir)//遍历虚拟目录
                                {
                                    if (!virtualDirectories.Contains(subItem.Name))//是否有重复的虚拟目录
                                    {
                                        try
                                        {
                                            //新建虚拟目录
                                            DirectoryEntry newVirDir = virEntry.Children.Add(subItem.Name, "IIsWebVirtualDir");
                                            newVirDir.Invoke("AppCreate", true);
                                            newVirDir.CommitChanges();
                                            virEntry.CommitChanges();
                                            UpdateDirInfo(newVirDir, subItem);
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine("新建虚拟目录失败!原因:" ex.ToString());
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("虚拟目录已经存在!");
                                    }
                                }

                            }
                        }

                    }
                }
            }
        /// <summary>
        /// 判断object对象是否为数组
        /// </summary>
        public static bool IsArray(object o)
        {
            return o is Array;
        }
        /// <summary>
        /// 获取虚拟目录集合
        /// </summary>
        private static VirtualDirectories GetVirDirs(DirectoryEntries des)
        {
            VirtualDirectories tmpdirs = new VirtualDirectories();
            foreach (DirectoryEntry de in des)
            {
                if (de.SchemaClassName == "IIsWebVirtualDir")
                {
                    VirtualDirectory vd = new VirtualDirectory();
                    vd.Name = de.Name;
                    vd.AccessRead = (bool)de.Properties["AccessRead"][0];
                    vd.AccessExecute = (bool)de.Properties["AccessExecute"][0];
                    vd.AccessWrite = (bool)de.Properties["AccessWrite"][0];
                    vd.AnonymousUserName = (string)de.Properties["AnonymousUserName"][0];
                    vd.AnonymousUserPass = (string)de.Properties["AnonymousUserName"][0];
                    vd.AuthBasic = (bool)de.Properties["AuthBasic"][0];
                    vd.AuthNTLM = (bool)de.Properties["AuthNTLM"][0];
                    vd.ContentIndexed = (bool)de.Properties["ContentIndexed"][0];
                    vd.EnableDefaultDoc = (bool)de.Properties["EnableDefaultDoc"][0];
                    vd.EnableDirBrowsing = (bool)de.Properties["EnableDirBrowsing"][0];
                    vd.AccessSSL = (bool)de.Properties["AccessSSL"][0];
                    vd.AccessScript = (bool)de.Properties["AccessScript"][0];
                    vd.Path = (string)de.Properties["Path"][0];
                    vd.flag = 0;
                    vd.DefaultDoc = (string)de.Properties["DefaultDoc"][0];
                    tmpdirs.Add(vd.Name, vd);
                }
            }
            return tmpdirs;
        }

标签:IIS站点

关于我

喜欢简简单单的生活.

记录平凡世界的点滴

-于2010年12月31日

联系我

  • 微信:fishren
  • 邮箱:
    tanlixiao@163.com
  • Address:
    中国最南端的滨海城市
    河西区 解放路 502