using system;
using system.directoryservices;
using system.collections;
using system.text.regularexpressions;
using system.text;
/**
* @author 吳海燕
* @email [email protected]
* 2004-6-25 第一版
*/
namespace wuhy.toolbox
{
/// <summary>
/// 這個類是靜態類。用來實現管理iis的基本操作。
/// 管理iis有兩種方式,一是adsi,一是wmi。由于系統限制的原因,只好選擇使用adsi實現功能。
/// 這是一個遺憾。只有等到只有使用iis 6的時候,才有可能使用wmi來管理系統
/// 不過有一個問題就是,我現在也覺得這樣的一個方法在本地執行會比較的好。最好不要遠程執行。
/// 因為那樣需要占用相當數量的帶寬,即使要遠程執行,也是推薦在同一個網段里面執行
/// </summary>
public class iisadminlib
{
#region username,password,hostname的定義
public static string hostname
{
get
{
return hostname;
}
set
{
hostname = value;
}
}
public static string username
{
get
{
return username;
}
set
{
username = value;
}
}
public static string password
{
get
{
return password;
}
set
{
if(username.length <= 1)
{
throw new argumentexception("還沒有指定好用戶名。請先指定用戶名");
}
password = value;
}
}
public static void remoteconfig(string hostname, string username, string password)
{
hostname = hostname;
username = username;
password = password;
}
private static string hostname = "localhost";
private static string username;
private static string password;
#endregion
#region 根據路徑構造entry的方法
/// <summary>
/// 根據是否有用戶名來判斷是否是遠程服務器。
/// 然后再構造出不同的directoryentry出來
/// </summary>
/// <param name="entpath">directoryentry的路徑</param>
/// <returns>返回的是directoryentry實例</returns>
public static directoryentry getdirectoryentry(string entpath)
{
directoryentry ent;
if(username == null)
{
ent = new directoryentry(entpath);
}
else
{
// ent = new directoryentry(entpath, hostname+"http://"+username, password, authenticationtypes.secure);
ent = new directoryentry(entpath, username, password, authenticationtypes.secure);
}
return ent;
}
#endregion
#region 添加,刪除網站的方法
/// <summary>
/// 創建一個新的網站。根據傳過來的信息進行配置
/// </summary>
/// <param name="siteinfo">存儲的是新網站的信息</param>
public static void createnewwebsite(newwebsiteinfo siteinfo)
{
if(! ensurenewsiteenavaible(siteinfo.bindstring))
{
throw new duplicatedwebsiteexception("已經有了這樣的網站了。" + environment.newline + siteinfo.bindstring);
}
string entpath = string.format("iis://{0}/w3svc", hostname);
directoryentry rootentry = getdirectoryentry(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();
}
/// <summary>
/// 刪除一個網站。根據網站名稱刪除。
/// </summary>
/// <param name="sitename">網站名稱</param>
public static void deletewebsitebyname(string sitename)
{
string sitenum = getwebsitenum(sitename);
string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum);
directoryentry siteentry = getdirectoryentry(siteentpath);
string rootpath = string.format("iis://{0}/w3svc", hostname);
directoryentry rootentry = getdirectoryentry(rootpath);
rootentry.children.remove(siteentry);
rootentry.commitchanges();
}
#endregion
#region start和stop網站的方法
public static void startwebsite(string sitename)
{
string sitenum = getwebsitenum(sitename);
string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum);
directoryentry siteentry = getdirectoryentry(siteentpath);
siteentry.invoke("start", new object[] {});
}
public static void stopwebsite(string sitename)
{
string sitenum = getwebsitenum(sitename);
string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum);
directoryentry siteentry = getdirectoryentry(siteentpath);
siteentry.invoke("stop", new object[] {});
}
#endregion
#region 確認網站是否相同
/// <summary>
/// 確定一個新的網站與現有的網站沒有相同的。
/// 這樣防止將非法的數據存放到iis里面去
/// </summary>
/// <param name="bindstr">網站邦定信息</param>
/// <returns>真為可以創建,假為不可以創建</returns>
public static bool ensurenewsiteenavaible(string bindstr)
{
string entpath = string.format("iis://{0}/w3svc", hostname);
directoryentry ent = getdirectoryentry(entpath);
foreach(directoryentry child in ent.children)
{
if(child.schemaclassname == "iiswebserver")
{
if(child.properties["serverbindings"].value != null)
{
if(child.properties["serverbindings"].value.tostring() == bindstr)
{
return false;
}
}
}
}
return true;
}
#endregion
#region 獲取一個網站編號的方法
/// <summary>
/// 獲取一個網站的編號。根據網站的serverbindings或者servercomment來確定網站編號
/// </summary>
/// <param name="sitename"></param>
/// <returns>返回網站的編號</returns>
/// <exception cref="notfoundwebsiteexception">表示沒有找到網站</exception>
public static string getwebsitenum(string sitename)
{
regex regex = new regex(sitename);
string tmpstr;
string entpath = string.format("iis://{0}/w3svc", hostname);
directoryentry ent = getdirectoryentry(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;
}
}
}
}
throw new notfoundwebsiteexception("沒有找到我們想要的站點" + sitename);
}
#endregion
#region 獲取新網站id的方法
/// <summary>
/// 獲取網站系統里面可以使用的最小的id。
/// 這是因為每個網站都需要有一個唯一的編號,而且這個編號越小越好。
/// 這里面的算法經過了測試是沒有問題的。
/// </summary>
/// <returns>最小的id</returns>
public static string getnewwebsiteid()
{
arraylist list = new arraylist();
string tmpstr;
string entpath = string.format("iis://{0}/w3svc", hostname);
directoryentry ent = getdirectoryentry(entpath);
foreach(directoryentry child in ent.children)
{
if(child.schemaclassname == "iiswebserver")
{
tmpstr = child.name.tostring();
list.add(convert.toint32(tmpstr));
}
}
list.sort();
int i = 1;
foreach(int j in list)
{
if(i == j)
{
i++;
}
}
return i.tostring();
}
#endregion
}
#region 新網站信息結構體
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
}
新聞熱點
疑難解答
圖片精選