用C#操縱IIS
2024-07-15 01:59:25
供稿:網(wǎng)友
using System;
using System.DirectoryServices;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
/**
* @author 吳海燕
* @email wuhy80-usual@yahoo.com
* 2004-6-25 第一版
*/
namespace Wuhy.ToolBox
{
/// <summary>
/// 這個(gè)類(lèi)是靜態(tài)類(lèi)。用來(lái)實(shí)現(xiàn)管理IIS的基本操作。
/// 管理IIS有兩種方式,一是ADSI,一是WMI。由于系統(tǒng)限制的原因,只好選擇使用ADSI實(shí)現(xiàn)功能。
/// 這是一個(gè)遺憾。只有等到只有使用IIS 6的時(shí)候,才有可能使用WMI來(lái)管理系統(tǒng)
/// 不過(guò)有一個(gè)問(wèn)題就是,我現(xiàn)在也覺(jué)得這樣的一個(gè)方法在本地執(zhí)行會(huì)比較的好。最好不要遠(yuǎn)程執(zhí)行。
/// 因?yàn)槟菢有枰加孟喈?dāng)數(shù)量的帶寬,即使要遠(yuǎn)程執(zhí)行,也是推薦在同一個(gè)網(wǎng)段里面執(zhí)行
/// </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("還沒(méi)有指定好用戶(hù)名。請(qǐng)先指定用戶(hù)名");
}
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 根據(jù)路徑構(gòu)造Entry的方法
/// <summary>
/// 根據(jù)是否有用戶(hù)名來(lái)判斷是否是遠(yuǎn)程服務(wù)器。
/// 然后再構(gòu)造出不同的DirectoryEntry出來(lái)
/// </summary>
/// <param name="entPath">DirectoryEntry的路徑</param>
/// <returns>返回的是DirectoryEntry實(shí)例</returns>
public static DirectoryEntry GetDirectoryEntry(string entPath)
{
DirectoryEntry ent;
if(UserName == null)
{
ent = new DirectoryEntry(entPath);
}
else
{
// ent = new DirectoryEntry(entPath, HostName+"//"+UserName, Password, AuthenticationTypes.Secure);
ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);
}
return ent;
}
#endregion
#region 添加,刪除網(wǎng)站的方法
/// <summary>
/// 創(chuàng)建一個(gè)新的網(wǎng)站。根據(jù)傳過(guò)來(lái)的信息進(jìn)行配置