`
hugh-lin
  • 浏览: 70362 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

[转]IIS站点管理类

阅读更多
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.Text.RegularExpressions;
using System.Collections;

namespace IISControlService
{
    /**//// <summary>
    /// 这个类是静态类。用来实现管理IIS的基本操作。
    /// </summary>
    public class IISManager
    {
        #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>
        /// 创建一个新的网站。根据传过来的信息进行配置
        /// </summary>
        /// <param name="siteInfo">存储的是新网站的信息</param>
        public static string CreateNewWebSite(NewWebSiteInfo siteInfo)
        {
            if (!EnsureNewSiteEnavaible(siteInfo.BindString))
            {
                throw new SiteExistException("已经有了这样的网站了。" + Environment.NewLine + siteInfo.BindString);
            }

            string entPath = String.Format("IIS://{0}/w3svc", HostName);
            DirectoryEntry rootEntry = GetDirectoryEntry(entPath);

            string newSiteNum = GetNewWebSiteID();
            DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");
            newSiteEntry.CommitChanges();

            //设置站点IP地址、端口、主机头
            newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindString;
            //设置站点名称
            newSiteEntry.Properties["ServerComment"].Value = siteInfo.CommentOfWebSite;
            //设置站点的访问权限
            newSiteEntry.Properties["AccessFlags"].Value = 512 | 1;
            newSiteEntry.CommitChanges();

            DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
            vdEntry.CommitChanges();

            //创建应用程序,并指定应用程序池为"HostPool","true"表示如果HostPool不存在,则自动创建
            vdEntry.Invoke("AppCreate3", new object[]{ 2, "HostPool", true });
            vdEntry.Properties["Path"].Value = siteInfo.WebPath;
            //设置应用程序名称
            vdEntry.Properties["AppFriendlyName"].Value = "HostCreator";
            vdEntry.CommitChanges();

            return newSiteNum;
        }

        /**//// <summary>
        /// 删除一个网站。根据网站名称删除。
        /// </summary>
        /// <param name="siteName">网站名称</param>
        public static void DeleteWebSiteByName(string siteName)
        {
            string siteNum = GetWebSiteNum(siteName);
            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);

            string rootPath = String.Format("IIS://{0}/w3svc", HostName);
            DirectoryEntry rootEntry = GetDirectoryEntry(rootPath);

            rootEntry.Children.Remove(siteEntry);
            rootEntry.CommitChanges();
        }
        #endregion

        #region Start和Stop网站的方法
        public static void StartWebSite(string siteName)
        {
            string siteNum = GetWebSiteNum(siteName);
            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);

            siteEntry.Invoke("Start", new object[] { });
        }

        public static void StopWebSite(string siteName)
        {
            string siteNum = GetWebSiteNum(siteName);
            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);

            siteEntry.Invoke("Stop", new object[] { });
        }
        #endregion

        #region 确认网站是否相同
        /**//// <summary>
        /// 确定一个新的网站与现有的网站没有相同的。
        /// 这样防止将非法的数据存放到IIS里面去
        /// </summary>
        /// <param name="bindStr">网站邦定信息</param>
        /// <returns>真为可以创建,假为不可以创建</returns>
        public static bool EnsureNewSiteEnavaible(string bindStr)
        {
            string entPath = String.Format("IIS://{0}/w3svc", HostName);
            DirectoryEntry ent = GetDirectoryEntry(entPath);

            foreach (DirectoryEntry child in ent.Children)
            {
                if (child.SchemaClassName == "IIsWebServer")
                {
                    if (child.Properties["ServerBindings"].Value != null)
                    {
                        if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }
        #endregion

        #region 获取一个网站编号的方法
        /**//// <summary>
        /// 获取一个网站的编号。根据网站的ServerBindings或者ServerComment来确定网站编号
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns>返回网站的编号</returns>
        /// <exception cref="NotFoundWebSiteException">表示没有找到网站</exception>
        public static string GetWebSiteNum(string siteName)
        {
            Regex regex = new Regex(siteName);
            string tmpStr;

            string entPath = String.Format("IIS://{0}/w3svc", HostName);
            DirectoryEntry ent = GetDirectoryEntry(entPath);

            foreach (DirectoryEntry child in ent.Children)
            {
                if (child.SchemaClassName == "IIsWebServer")
                {
                    if (child.Properties["ServerBindings"].Value != null)
                    {
                        tmpStr = child.Properties["ServerBindings"].Value.ToString();
                        if (regex.Match(tmpStr).Success)
                        {
                            return child.Name;
                        }
                    }

                    if (child.Properties["ServerComment"].Value != null)
                    {
                        tmpStr = child.Properties["ServerComment"].Value.ToString();
                        if (regex.Match(tmpStr).Success)
                        {
                            return child.Name;
                        }
                    }
                }
            }

            throw new Exception("没有找到我们想要的站点" + siteName);
        }
        #endregion

        #region 获取新网站id的方法
        /**//// <summary>
        /// 获取网站系统里面可以使用的最小的ID。
        /// 这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。
        /// 这里面的算法经过了测试是没有问题的。
        /// </summary>
        /// <returns>最小的id</returns>
        public static string GetNewWebSiteID()
        {
            ArrayList list = new ArrayList();
            string tmpStr;

            string entPath = String.Format("IIS://{0}/w3svc", HostName);
            DirectoryEntry ent = GetDirectoryEntry(entPath);

            foreach (DirectoryEntry child in ent.Children)
            {
                if (child.SchemaClassName == "IIsWebServer")
                {
                    tmpStr = child.Name.ToString();
                    list.Add(Convert.ToInt32(tmpStr));
                }
            }

            list.Sort();

            int i = 1;
            foreach (int j in list)
            {
                if (i == j)
                {
                    i++;
                }
            }

            return i.ToString();
        }
        #endregion
    }

    #region 新网站信息结构体
    public struct NewWebSiteInfo
    {
        private string hostIP; // The Hosts IP Address
        private string portNum; // The New Web Sites Port.generally is "80"
        private string descOfWebSite; // 网站主机头。例如"www.dns.com.cn"
        private string commentOfWebSite;// 网站注释。一般也为网站的网站名。
        private string webPath; // 网站的主目录。例如"e:\tmp"

        public NewWebSiteInfo(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath)
        {
            this.hostIP = hostIP;
            this.portNum = portNum;
            this.descOfWebSite = descOfWebSite;
            this.commentOfWebSite = commentOfWebSite;
            this.webPath = webPath;
        }

        public string BindString
        {
            get
            {
                return String.Format("{0}:{1}:{2}", hostIP, portNum, descOfWebSite);
            }
        }

        public string CommentOfWebSite
        {
            get
            {
                return commentOfWebSite;
            }
        }

        public string WebPath
        {
            get
            {
                return webPath;
            }
        }
    }
    #endregion

    public class SiteExistException : Exception
    {
        public SiteExistException(string message) : base(message) { }
    }
}
分享到:
评论

相关推荐

    IIS管理类-创建站点、虚拟目录,删除虚拟目录

    IIS管理类,可以实现创建站点、虚拟目录,删除虚拟目录等

    IIS控制类,操作IIS管理类

    功能描述: 1、生成一个新的站点 2、获得匿名用户 3、添加一个虚拟目录 4、更新一个虚拟目录 5、批量更新 6、连接服务器

    phpStudy for IIS (php一键安装包 for IIS) v2016.11.03

    有专门的控制面板,并且可以通过软件上的站点域名管理给IIS添加站点并自动配好php。软件自带php5.2+php5.3+php5.4支持一键切换。  软件集成IIS+php5.2/5.3/5.4+mysql+phpmyadmin+sql-front+ZendOptimizer;没有...

    IIS6.0 IIS,互联网信息服务

    转到“目录安全性”窗口,单击“IP地址及域名限制”下的“编辑”按钮,点选中“授权访问”以能接受客户端从本机之外的地方对IIS进行管理;最后单击“确定”按钮。 3.则在任意计算机的浏览器中输入如...

    纵横IIS链接嗅探器 v3.6

    纵横IIS链接嗅探器 1. 可以针对不同站点设置反盗链的过滤, 防止图片、桌面、软件、音乐、电影被人引用。如果发现请求者是盗用网站链接, 则自动重定向到错误处理页面 2. 能够指定信任站点,允许链接。 3. 纵横IIS...

    易网IIS v6.0管理组件 v2.1.1 免费版

    适用于IIS v5.0/v6.0实现通过各种WEB脚IIS管理站点,适用于虚拟主机和个人主页站点,结合FTP可以实现即时开通空间,与v1.0相比此版本做了许多改进功能说明:1、建立站点默认使用80端口,可以自定义域名,网站名称,...

    呆呆iis管理组件 v4.17

    海纳网iis管理组件 的升级版,可以远程页面管理iis v6.0,更新内容:支持servuftp的管理。 支持文件目录的权限管理。 支持站点系统用户的管理与绑定和修改。 支持更多站点属性的管理。 可以建立免受FSO困扰的站点。 ...

    PSMLIB IIS/FTP在线管理组件 v3.2.0.10

    程序主要功能:本免费组件可以为所有支持COM组件调用的脚本语言使用,如asp,php,c#等,并提供完整的API帮助文档,可以轻松使用接口完成WEB在线实时开通IIS站点/FTP空间提供虚拟主机服务。 IIS操作类支持serv_u5/6/7...

    AUTOHOST IIS 管理组件(AutoDll) v1.3

    组件功能:组件采用COM+方式安装,免去以往需ADMINISTRATOR帐号才可以开通站点而带来的安全问题站点创建,删除,编辑等Serv-U用户采用注册表存放改写域名绑检测部分,使其更正确人性化的站点目录权限重置功能,以前...

    虚拟主机监控系统 v1.1

    集IIS站点的CPU实时监控和限制、IIS文件限制、IIS防盗链、IIS文件限制功能、IIS线程及速度限制、SQL防注入、防ASP木马功能、带宽流量监控、连接数监控、站点信息监控、反黄过滤、URL过滤、站点备案管理于一身,功能...

    IIS管理模块1.1正式版-易语言

    查询网站_站点名() 2.IIS7.修改状态码网页(, ) 3.IIS7.输出XML() 4.IIS7.输出配置() 5.IIS7.删除状态码() 6.IIS7.启用全局IPv6() 7.IIS7.删除域名(, ) ------------这个命令第一版的时候有人发了评论说没有 我解释...

    IIS总管图形版 v1.0 Beta

    提供了微软的IIS未提供的一些批量管理功能功能介绍:1.批量备份站点2.通过备份文件批量恢复站点3.批量修改站点IP4.批量删除站点

    搭建多个Web站点(windows)

    本教程适用于熟悉Windows操作系统,希望合理利用资源、统一管理站点以提高运维效率的用户。比如,您可以在一台云服务器上配置多个不同分类的博客平台或者搭建多个Web站点实现复杂业务的网站系统。 教程中,将通过...

    搜易驾校管理系统 v1.2

    二,运行环境:IIS + .NET FRAMEWORK 2.0 + SQL SERVER 2005 + ASP.NET三,安装步骤 2.1,把数据库文件里的对应数据库文件附加到SQL SERVER 2005,然后新建一个IIS站点,设置支持.NET 2.0 2.2,然后将程序文件复制...

    服务器上开设多个站点.doc

    第二步:建立WEB站点 1、 控制面板 – 管理工具打开Internet 信息服务(IIS)管理器。 2、开始添加站点,在 网站 &gt; 上点击鼠标右键 新建 &gt; -- 网站 &gt; 3、点击 下一步 &gt; 以后,输入站点描述,只是在IIS里面的一个识别...

    仿赶集网站模板.net分类信息管理系统下载

    IIS 5.0 及以上版本(推荐 IIS6.0) 分类信息发布系统是一个完善的分类信息发布网站系统,供求信息网站发布系统子模块,充分考虑了分类信息网站,供求信息网站管理系统的通用需求。全后台管理方式,后台功能齐全,使用...

    HzexeIISManager v2.0

    用于虚拟主机管理类的服务器类软件,系统介绍如下:〖WEB站点管理〗可设置匿名访问用户在网站目录的读取、写入、读取写入、更改、完全控制的权限,防FSO支持多主机头、端口、IP定义新站点初始化首页设置站点过期时间...

    西亚IIS自动开通组件 v1.0.118 稳定版

    实现功能:建立站点成功返回本次建立的站点序号(范围:1-100000,方便管理,返回100001为目录站点已经存在;返回100002为端口冲突1:用户只需在asp文件中指定站点名、站点根目录、站点IP地址、站点域名2:程序自动...

    生活分类信息发布网站的优秀网站管理系统正式版

    IIS 5.0 及以上版本(推荐 IIS6.0) 网软志成分类信息网.NET综合风格正式版下载,能帮助用户建立一个像赶集网的专业分类信息网站门户欢迎大家下载使用。是一套网软志成免费提供的分类供求信息发布网站商业建站程序。...

    西部数码网站管理助手 v3.0.0

    1.一键添加IIS站点、FTP帐号、建网站目录、开Mysql数据库。可选支持asp,php,asp.net等脚本,各站点独立权限。 2.Mysql数据库创建、导入、导出、修复、改密码等 3.Mssql数据库创建、导入、备份、修复所有者、改密码...

Global site tag (gtag.js) - Google Analytics