轉載請註明出處:
原創作者: Jaron , CSDN IIS技術版主 2007/3/26
- CSDN
- 佳融軟件
只貼代碼,相關的描述有空時再補上。
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);
}
{
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);
}
}
}
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);
}
}
}
更新
處理主機頭綁定