日期:2011-07-18  浏览次数:20476 次

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 = GetW