程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#新建站點,刪除站點函數代碼

C#新建站點,刪除站點函數代碼

編輯:關於C語言

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


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved