转载请注明出处:
原创作者: Jaron , 优快云 IIS技术版主 2007/3/26
- 优快云
- 佳融软件
只贴代码,相关的描述有空时再补上。
private
void
BindServerBindings(
string
Action,
int
SiteID,
string
vHeader,
string
IPAddress,
int
Port,
string
url)

...
{
WebSiteInfo _NewWebSiteInfo = new WebSiteInfo();
IISHelperObj = new IISHelper("LOCALHOST");
WebSites website = new WebSites();
IISHelperObj.IsCalculateDirectory = true;
website = IISHelperObj.GetWebSites(SiteID);
foreach (WebSiteInfo _wsi in website)

...{
ServerBindings _NewServerBinding = new ServerBindings();
ServerBindings sbd = _wsi.ServerBindings;
foreach (ServerBinding _sb in sbd)

...{
//删除操作
if (Action == "Delete")

...{
if (_sb.Header == vHeader && _sb.Port == Port)

...{
}
else

...{
_NewServerBinding.Add(_sb);
}
}
}
_NewWebSiteInfo.WebSiteID = SiteID;
_NewWebSiteInfo.ServerBindings = _NewServerBinding;
}
ServerBindings s = _NewWebSiteInfo.ServerBindings;
IISHelperObj.SetWebSite(_NewWebSiteInfo, SiteID, true);
Response.Redirect("Succeed.Aspx?Url=" + url);
}
ServerBinding 类
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Collections;
using
System.DirectoryServices;

namespace
JaronSoft.Library.Networks.IIS

...
{

/**//// <summary>
/// 站点绑定
/// 单行值 127.0.0.1:www.jaronsoft.cn:80
/// </summary>
[Serializable()]
public class ServerBinding

...{
private int _Port = 80;
private string _Header;
private string _IPAddress = "";


/**//// <summary>
/// 端口
/// </summary>
public int Port

...{

get ...{ return _Port; }

set ...{ _Port = value; }
}


/**//// <summary>
/// 主机头
/// </summary>
public string Header

...{

get ...{ return _Header; }

set ...{ _Header = value; }
}


/**//// <summary>
/// IP 地址
/// </summary>
public string IPAddress

...{

get ...{ return _IPAddress; }

set ...{ _IPAddress = value; }
}


/**//// <summary>
/// 得到端口
/// </summary>
/// <returns></returns>
public int GetPort()

...{
string[] bindings = this.AdsiValue.Split(':');
return int.Parse(bindings[1]);
}


/**//// <summary>
/// 得到主机头
/// </summary>
/// <returns></returns>
public string GetHeader()

...{
string[] bindings = this.AdsiValue.Split(':');
return bindings[2];
}


/**//// <summary>
/// 得到IP地址
/// </summary>
/// <returns></returns>
public string GetIPAddress()

...{
string[] bindings = this.AdsiValue.Split(':');
return bindings[0];
}
}


/**//// <summary>
/// 数据格式
/// (ServerBinding)127.0.0.1:www.jaronsoft.cn:80
/// (ServerBinding)127.0.0.1:iis.jaronsoft.cn:80
/// </summary>
[Serializable()]
public class ServerBindings : CollectionBase

...{

/**//// <summary>
/// this
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public ServerBinding this[int index]

...{
get

...{
return ((ServerBinding)List[index]);
}
set

...{
List[index] = value;
}
}

/**//// <summary>
/// 添加
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public void Add(ServerBinding value)

...{
List.Add(value);
}


/**//// <summary>
/// 索引
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public ServerBinding IndexOf(int index)

...{
return (ServerBinding)List[index];
}


/**//// <summary>
/// 删除
/// </summary>
/// <param name="value"></param>
public void Remove(ServerBinding value)

...{
List.Remove(value);
}

}
}
更新
处理主机头绑定
#region 处理主机头绑定
Object[] newList = new Object[newSiteInfo.ServerBindings.Count];
for (int i = 0; i < newSiteInfo.ServerBindings.Count; i++)

...{
string headerStr = string.Format("{0}:{1}:{2}", newSiteInfo.ServerBindings[i].IPAddress, newSiteInfo.ServerBindings[i].Port, newSiteInfo.ServerBindings[i].Header);
newList[i] = headerStr;
}
newSiteEntry.Properties["ServerBindings"].Value = newList;
#endregion