用C#操縱IIS
2024-07-16 17:44:52
供稿:網(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>
/// 這個類是靜態(tài)類。用來實(shí)現(xiàn)管理IIS的基本操作。
/// 管理IIS有兩種方式,一是ADSI,一是WMI。由于系統(tǒng)限制的原因,只好選擇使用ADSI實(shí)現(xiàn)功能。
/// 這是一個遺憾。只有等到只有使用IIS 6的時候,才有可能使用WMI來管理系統(tǒng)
/// 不過有一個問題就是,我現(xiàn)在也覺得這樣的一個方法在本地執(zhí)行會比較的好。最好不要遠(yuǎn)程執(zhí)行。
/// 因?yàn)槟菢有枰加孟喈?dāng)數(shù)量的帶寬,即使要遠(yuǎn)程執(zhí)行,也是推薦在同一個網(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("還沒有指定好用戶名。請先指定用戶名");
}
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ù)是否有用戶名來判斷是否是遠(yuǎn)程服務(wù)器。
/// 然后再構(gòu)造出不同的DirectoryEntry出來
/// </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)建一個新的網(wǎng)站。根據(jù)傳過來的信息進(jìn)行配置