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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值