首页 > 开发 > 综合 > 正文

C#新建站点,删除站点函数代码

2024-07-21 02:25:57
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.windows.forms.design;
using system.directoryservices;
using system.reflection;
using system.text.regularexpressions;

//添加站点代码

private void button2_click(object sender, system.eventargs e)
  {
   
   
   string newservercomment=textbox1.text;   
   string newserverip=textbox2.text;
   string newserverport=textbox3.text;   
   string newserverpath=textbox4.text;
   string newserverheader=textbox5.text;   
   //newwebsiteinfo siteinfo=new newwebsiteinfo(hostip,portnum,descofwebsite,commentofwebsite,webpath);
   newwebsiteinfo siteinfo=new newwebsiteinfo(newserverip,newserverport,newserverheader,newservercomment,newserverpath);
   string entpath = "iis://localhost/w3svc";
   directoryentry rootentry = new directoryentry(entpath);


   string newsitenum = getnewwebsiteid();

   directoryentry newsiteentry = rootentry.children.add(newsitenum, "iiswebserver");

   newsiteentry.commitchanges();

   newsiteentry.properties["serverbindings"].value = siteinfo.bindstring;

   newsiteentry.properties["servercomment"].value = siteinfo.commentofwebsite;

   newsiteentry.commitchanges();

   directoryentry vdentry = newsiteentry.children.add("root", "iiswebvirtualdir");

   vdentry.commitchanges();

   vdentry.properties["path"].value = siteinfo.webpath;

   vdentry.commitchanges();
   
    

   messagebox.show("站点"+siteinfo.commentofwebsite+"创建完成");
   

  }

//iis站点查询代码
  //// <summary>
  /// get and return a new website id of specify host
  /// </summary>
  /// <returns>the smallest new website id of the host</returns>
  public string getnewwebsiteid()
  {
   arraylist idlist = new arraylist();
   string tmpstr;

   string entrypath = "iis://localhost/w3svc";
   directoryentry entry = getdirectoryentry(entrypath);
  
   foreach (directoryentry child in entry.children)
   {
    if (child.schemaclassname == "iiswebserver")
    {
     tmpstr = child.name.tostring();
     idlist.add(convert.toint32(tmpstr));
    }
   }

   idlist.sort();

   int i = 1;
   foreach (int id in idlist)
   {
    if (i == id)
    {
     i++;
    }
   }

   return i.tostring();
  }

//删除站点代码

private void button3_click(object sender, system.eventargs e)
  {
   string newservercomment=textbox1.text;   
   string newserverip=textbox2.text;
   string newserverport=textbox3.text;   
   string newserverpath=textbox4.text;
   string newserverheader=textbox5.text;
   string newserverhost=textbox6.text;

   string sitenum = getwebsitenum(newservercomment);
   string rootpath = "iis://localhost/w3svc";
   string siteentpath =rootpath+"/"+sitenum; 
   directoryentry rootentry = getdirectoryentry(rootpath);
   directoryentry siteentry = getdirectoryentry(siteentpath);
   rootentry.children.remove(siteentry);
   rootentry.commitchanges();
   messagebox.show("站点 "+newservercomment+" 删除完毕");
  }

  //根据站点名称获取站点标识符
  #region 获取一个网站编号的方法
  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 "";
   
  }
  #endregion

 

#region 新iis站点信息结构体

  public struct newwebsiteinfo
  {
   private string hostip;   // the hosts ip address

   private string portnum;   // the new web sites port.generally is "80"

   private string descofwebsite; // 网站表示。一般为网站的网站名。例如"www.dns.com.cn"

   private string commentofwebsite;// 网站注释。一般也为网站的网站名。

   private string webpath;   // 网站的主目录。例如"e:/tmp"

   public newwebsiteinfo(string hostip, string portnum, string descofwebsite, string commentofwebsite, string webpath)

   {

    this.hostip = hostip;

    this.portnum = portnum;

    this.descofwebsite = descofwebsite;

    this.commentofwebsite = commentofwebsite;

    this.webpath = webpath;

   }


   public string bindstring

   {

    get
    {
     return string.format("{0}:{1}:{2}", hostip, portnum, descofwebsite);

    }

   }

   public string commentofwebsite  {get{return commentofwebsite;}}

   public string webpath  {get{return webpath;}}

  }

  #endregion


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