C#建立網站,刪除網站函數代碼

來源:互聯網
上載者:User
函數|網站

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




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.