用C#操縱IIS
2024-07-16 17:44:52
供稿:網友
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>
/// 這個類是靜態類。用來實現管理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+"//"+UserName, Password, AuthenticationTypes.Secure);
ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);
}
return ent;
}
#endregion
#region 添加,刪除網站的方法
/// <summary>
/// 創建一個新的網站。根據傳過來的信息進行配置