private static string hostName = "localhost";
private static string userName = "qf";
private static string password = "qinfei";
#endregion
#region 根据路径构造 Entry 的方法 根据路径构造 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 添加,删除网站的方法 添加,删除网站的方法
public static void CreateNewWebSite(string hostIP, string portNum, string
descOfWebSite, string commentOfWebSite, string webPath)
{
if (!EnsureNewSiteEnavaible(hostIP + portNum + descOfWebSite))
{
throw new ArgumentNullException("已经有了这样的网站了。" +
Environment.NewLine + hostIP + portNum + descOfWebSite);
}
string entPath = String.Format("IIS://{0}/w3svc", HostName);
DirectoryEntry rootEntry = GetDirectoryEntry(entPath);//取得 iis 路径
string newSiteNum = GetNewWebSiteID(); //取得新网站 ID
DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum,
"IIsWebServer"); //增加站点
newSiteEntry.CommitChanges();//保存对区域的更改(这里对站点的更改)