二。IIS虚拟目录管理类 /// <summary> /// IIS虚拟目录管理类 /// </summary> public class IISVirDirManager { // 操作IIS时,Path的格式为IIS://ServerName(IP)/ServiceType/SiteID/Directory private string KeyType; // 虚拟目录类型,IIsWebVirtualDir或IIsFtpVirtualDir private string SiteID; // 站点标识符 private string RootPath; // IIS Web|Ftp默认站点根目录 private DirectoryEntry RootDE; // IIS Web|Ftp默认站点 /// <summary> /// 连接IIS服务器,获取Web|Ftp默认站点根目录入口 /// </summary> private void ConnectToServer() { string SVCType; if (KeyType == "IIsWebVirtualDir") { SVCType = "W3SVC"; } else if (KeyType == "IIsFtpVirtualDir") { SVCType = "MSFTPSVC"; } else { throw new Exception("Please check the virtual directory type!"); } RootPath = "IIS://localhost/" + SVCType + "/" + SiteID + "/ROOT"; RootDE = new DirectoryEntry(RootPath); } /// <summary>构造函数1</summary> public IISVirDirManager(string strKeyType) { SiteID = "1"; KeyType = strKeyType; ConnectToServer(); } /// <summary>构造函数2</summary> /// <param name = "strWebSiteID">站点标识符</param> public IISVirDirManager(string strKeyType, string strWebSiteID) { SiteID = strWebSiteID; KeyType = strKeyType; ConnectToServer(); } /// <summary> /// 判断IIS默认站点下是否已经存在该虚拟目录 /// </summary> /// <param name = "strVirDirName">虚拟目录名称</param> /// <returns> /// 如果已经存在该虚拟目录,返回true;否则,返回false。 /// </returns> private bool Exists(string strVirDirName) { string strWebSitePath = RootPath + "/" + strVirDirName; if (DirectoryEntry.Exists(strWebSitePath)) { return true; } else { return false; } } /// <summary> /// 查找IIS下名称为strVirDir的Web|Ftp虚拟目录 /// </summary> /// <param name = "strVirDirName">虚拟目录名称</param> /// <returns> /// 如果查找成功,返回查找到的虚拟目录入口,否则返回null。 /// </returns> private DirectoryEntry GetVirDir(string strVirDirName) { DirectoryEntry de = null; if (Exists(strVirDirName)) { de = RootDE.Children.Find(strVirDirName, KeyType); } return de; } /// <summary> /// 创建Web|Ftp虚拟目录 /// </summary> /// <param name = "VD"> /// 所创建虚拟目录的属性集。创建目录之前,先设置相关属性。 /// </param> /// <returns> /// 无返回值。<br/> /// 如果已经存在同名的虚拟目录,则抛出异常。注意在调用处捕获该异常。 /// </returns> public void CreateVirDir(VirtualDirectory VD) { if (Exists(VD.Name)) { throw new Exception("CreateVirDir Exception:Virtual directory already exists!"); } DirectoryEntry de = RootDE.Children.Add(VD.Name, KeyType); if (KeyType == "IIsWebVirtualDir") { de.Invoke("AppCreate", true); } de.CommitChanges(); RootDE.CommitChanges(); UpdateDirInfo(de, VD);// 创建后设置属性 } 上一篇《基于DirectoryServices的IIS虚拟目录管理(2)》 下一篇《基于DirectoryServices的IIS虚拟目录管理(4)》

评论