获得IIS节点信息

来源:互联网 发布:淘宝证书安装 编辑:程序博客网 时间:2024/05/22 13:40
  
  public static List<string> GetNodeIp()
            {
                List<string> resultList=new List<string>();
                int TotalServerCount = 0;


                DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC");
                //TotalServerCount=rootfolder.Children.SchemaFilter.Count;


                foreach (DirectoryEntry child in rootfolder.Children)
                {
                    if (child.SchemaClassName == "IIsWebServer")
                    {
                        TotalServerCount += 1;
                    }
                }
                //循环获取所有站点详细属性写入数组中
                string[] arrayServerID = new string[TotalServerCount]; //站点标识符
                string[] arrayServerIP = new string[TotalServerCount]; //站点主机头 
                string[] arrayServerPort = new string[TotalServerCount]; //站点主机头 
                string[] arrayServerHeader = new string[TotalServerCount]; //站点主机头
                string[] arrayServerPath = new string[TotalServerCount]; //站点主机头 
                string[] arrayServerComment = new string[TotalServerCount]; //站点主机头
                string[] arrayServerBinds = new string[TotalServerCount]; //站点主机头


                string currentServerBindings; //绑定主机头IP端口字符串
                char[] a = ":".ToCharArray();
                string[] currentBingdings = new string[2];
                int i = 0;
                foreach (DirectoryEntry child in rootfolder.Children)
                {
                    if (child.SchemaClassName == "IIsWebServer")
                    {
                        arrayServerID.SetValue(child.Name.ToString(), i);
                        arrayServerComment.SetValue(child.Properties["ServerComment"].Value.ToString(), i);
                        currentServerBindings = child.Properties["ServerBindings"].Value.ToString();


                        currentBingdings = currentServerBindings.Split(a);
                        arrayServerIP.SetValue(currentBingdings[0], i);
                        arrayServerPort.SetValue(currentBingdings[1], i);
                        arrayServerHeader.SetValue(currentBingdings[2], i);


                        foreach (DirectoryEntry rootChild in child.Children)
                        {
                            if ((rootChild.SchemaClassName == "IIsWebVirtualDir") && (rootChild.Name.ToString() == "root"))
                            {
                                if (rootChild.Properties["Path"].Value == null)
                                {
                                    arrayServerPath.SetValue("", i);
                                }
                                else
                                {
                                    arrayServerPath.SetValue(rootChild.Properties["Path"].Value.ToString(), i);
                                }
                            }
                        }


                        i += 1;
                    }
                }
                if (arrayServerIP.Length > 0)
                {
                    arrayServerIP.Where(x=>x!="").ForEach(x => {  resultList.Add(x); });
                }


                return resultList;
            }
原创粉丝点击