正文

基于DirectoryServices的IIS虚拟目录管理(3)2009-07-25 17:03:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/xman/45579.html

分享到:

二。IIS虚拟目录管理类

    /// <summary>

    /// IIS虚拟目录管理类

    /// </summary>

    public class IISVirDirManager

    {

        // 操作IIS时,Path的格式为IIS://ServerName(IP)/ServiceType/SiteID/Directory

 

        private string KeyType; // 虚拟目录类型,IIsWebVirtualDirIIsFtpVirtualDir

        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下名称为strVirDirWeb|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)

阅读(2481) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册