首页 > 网站 > IIS > 正文

c#读取IIS中的站点相关属性代码

2024-08-29 03:13:56
字体:
来源:转载
供稿:网友

private void combobox1_selectedindexchanged(object sender, system.eventargs e)
  {
   string currentservercomment=combobox1.selecteditem.tostring();
   string currentsitenum = getwebsitenum(currentservercomment);
   string rootpath = "iis://localhost/w3svc";
   string currentsitepath =rootpath+"/"+currentsitenum;   
   directoryentry siteentry = new directoryentry(currentsitepath); 

   string currentserverbindings=siteentry.properties["serverbindings"].value.tostring();
   char[] a=":".tochararray();
   string [] currentbingdings = null;   
   currentbingdings=currentserverbindings.split(a);
   string currentserverip=currentbingdings[0];
   string currentserverport=currentbingdings[1];
   string currentserverheader=currentbingdings[2];
   string currentserverhost="";
   string currentserverpath="";

   foreach (directoryentry child in siteentry.children)
   {
    
    if((child.schemaclassname == "iiswebvirtualdir")&&(child.name=="root"))
    {
     currentserverpath = child.properties["path"].value.tostring();
     
    }
   }
 

   textbox2.text=currentserverip;
   textbox3.text=currentserverport;
   textbox4.text=currentserverpath;
   textbox5.text=currentserverheader;
   textbox6.text=currentserverhost;
  }

/// <summary>
  /// 根据站点名称获取站点标识符
  /// </summary> 
  
  public string getwebsitenum(string sitename)
  {

   regex regex = new regex(sitename);
   string tmpstr;
   string entpath = "iis://localhost/w3svc";
   directoryentry ent =new directoryentry(entpath); 

   foreach(directoryentry child in ent.children)

   {
    if(child.schemaclassname == "iiswebserver")
    {
     if(child.properties["serverbindings"].value != null)
     {
      tmpstr = child.properties["serverbindings"].value.tostring();
      if(regex.match(tmpstr).success)
      {
       return child.name;
      }
     }

     if(child.properties["servercomment"].value != null)

     {
      tmpstr = child.properties["servercomment"].value.tostring();
      if(regex.match(tmpstr).success)
      {
       return child.name;
      }
     }
    }
   }
   return "";
   
  }



收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表