进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理。

【1】前提掌握要点:

(1)、IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0,其中IIS6.0 IIS7.0是在5.0的安全问题的基础上获得的发展,目前为止。6.0版本以后的都是比较安全稳定的,为什么需要了解IIS版本,是因为6.0以后和之前的IIS提供的操作API是不一样的,不过IIS6.0时代主要以using System.DirectoryServices空间下的DirectoryEntry 对象作为编程访问一个主要载体.但随着IIS7.0发布.NET的Web程序由IIS6.0开始逐渐过渡到 7.0版本.而且在编程控制IIS上新添加的Microsoft.Web.Administration名称空间, 可以操作7.0

(2)、平时的站点部署需要配置什么参数或者说是我们一般自己部署站点需要什么场景的操作,考虑可以用程序实现的,然后可以模型化为对象去实现。

我采用的方案是:将部署的站点声明为一个对象,其中包含了一般我们站点部署时设置的参数作为它的属性和字段,将我们一般站点设置实现为一些方法。

可以先定义一个站点类:

 
//站点身份验证模式
public enum autherRight {asp_net模拟, Form身份验证, Windows身份验证, 基本身份验证,匿名身份验证,摘要式身份验证 };
/// <summary>
/// 站点类
/// </summary>
public class newWebSiteInfo
{
    //站点设置参数
    public string hostIp;  //主机ip
    public string porNum;  //端口号
    public string hostName;//主机名
    public string webName; //网站名
    public string appName; //应用程序池
    public string webPath; //根目录
    public string visualPath;//虚拟目录
    public Dictionary<string, string> newMimeType;//需要新添加mime类型
    public autherRight autherRight;//身份验证模式
    public string defoultPage;//默认文档
 
    public newWebSiteInfo(string hostip, string portnum, string hostname, string webname, string appName, string webpath, string visualPath, Dictionary<string, string> newMimeType, autherRight autherRight,string defoultPage)
    {
        this.hostIp = hostip;
        this.porNum = portnum;
        this.hostName = hostname;
        this.webName=webname;
        this.appName = appName;
        this.webPath = webpath;
        this.visualPath = visualPath;
        this.newMimeType = newMimeType;
        this.autherRight = autherRight;
        this.defoultPage = defoultPage;
    }
    /// <summary>
    /// 返回站点绑定信息
    /// </summary>
    /// <returns></returns>
    public string bindString()
    {
            return String.IsNullOrEmpty(hostName) ? String.Format("http://{0}:{1}", hostIp, porNum) : String.Format("http://{0}:{1}", hostName, porNum);
    }
}

目前IIS操作主要有两种方式:一种是System.DirectoryServices空间下面的类,用于IIS5/6版本,和可以兼容iis6的IIS7版本;Microsoft.Web.Administration空间下面的类,IIS7引入的新的管理类,主要通过ServerManger类进行站点新增。

本次主要通过System.DirectoryServices方式操作IIS,也简要介绍一下ServerManger新建站点:

(1) 添加.net引用Microsoft.Web.Administration.dll,实例化ServerManger新增站点方式如下:

 
using System;
using Microsoft.Web.Administration;
 
class CreateASite
{
    static void Main(string[] args)
    {
        ServerManager serverManager = new ServerManager();
        Site mySite = serverManager.Sites.Add(
            "MySite", "d:\\inetpub\\mysite", 8080);
        mySite.ServerAutoStart = true;
        serverManager.CommitChanges();
    }
}

(2)通过System.DirectoryServices空间下面的类获取IIS服务,但其实对于高版本的也可以同时引用Microsoft.Web.Administration.dll中通过ServerManger来进行参数设置各有各的好处。

首先介绍一下我用到的几个命名空间:

 
using System.DirectoryServices;  //获取目录实体类
using Microsoft.Web.Administration; //ServerManger类所属命名空间using IISOle;                      //IIS管理添加mime类型
using System.Security.AccessControl; //设置文件安全权限类所属命名空间
using System.IO;                    //文件路径类所属命名空间
using System.ServiceProcess;        //上一节中所用的系统服务对象所属命名空间

现在提供我自己的源代码,可以适用一般类需求,对于一些特殊配置的应用程序部署,可以稍微做微调。

 
///created by george
///date:2014-5-3
///QQ:709617880
namespace IISmng
    //站点身份验证模式
    public enum autherRight {asp_net模拟, Form身份验证, Windows身份验证, 基本身份验证,匿名身份验证,摘要式身份验证 };
    /// <summary>
    /// 站点类
    /// </summary>
    public class newWebSiteInfo
    {
        //站点设置参数
        public string hostIp;  //主机ip
        public string porNum;  //端口号
        public string hostName;//主机名
        public string webName; //网站名
        public string appName; //应用程序池
        public string webPath; //物理路径
        public string visualPath;//虚拟目录
        public Dictionary<string, string> newMimeType;//需要新添加mime类型
        public autherRight autherRight;//身份验证模式
        public string defoultPage;//默认文档
 
        public newWebSiteInfo(string hostip, string portnum, string hostname, string webname, string appName, string webpath, string visualPath, Dictionary<string, string> newMimeType, autherRight autherRight,string defoultPage)
        {
            this.hostIp = hostip;
            this.porNum = portnum;
            this.hostName = hostname;
            this.webName=webname;
            this.appName = appName;
            this.webPath = webpath;
            this.visualPath = visualPath;
            this.newMimeType = newMimeType;
            this.autherRight = autherRight;
            this.defoultPage = defoultPage;
        }
        /// <summary>
        /// 返回站点绑定信息
        /// </summary>
        /// <returns></returns>
        public string bindString()
        {
                return String.IsNullOrEmpty(hostName) ? String.Format("http://{0}:{1}", hostIp, porNum) : String.Format("http://{0}:{1}", hostName, porNum);
        }
    }
    //托管模式
    public enum modelType{集成,经典};
    //net版本
    public enum netVersion{ v2_0 , v4_0};
    /// <summary>
    /// IIS操作类
    /// </summary>
    public class myIIS
    
        /// <summary>
        /// IIS版本属性
        /// </summary>
        public String IISVersion {
 
            get { return IISVersion; }
 
            set{
              DirectoryEntry IISService = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
              IISVersion = "v"+IISService.Properties["MajorIISVersionNumber"].Value.ToString();
            }
         
        }
 
        /// <summary>
        /// 检测客户端或服务器是否安装IIS服务
        /// </summary>
        /// <returns>true OR false</returns>
        public Boolean checkIIS() {
            Boolean retMsg = false;
            try {
                 DirectoryEntry IISService = new DirectoryEntry("IIS://localhost/W3SVC");
                 if(IISService.GetType().ToString().Equals("DirectoryEntry"))
                 {
                     if (checkServiceIsRunning("IIS Admin Service"))
                            retMsg=true;
                 }
            }
            catch(Exception e){
 
            }
            return retMsg;
        }
 
        /// <summary>
        /// 检测服务是否开启
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public Boolean checkServiceIsRunning(string serviceName)
        {
            ServiceController[] allServices = System.ServiceProcess.ServiceController.GetServices();
            Boolean runing = false;
            foreach (ServiceController sc in allServices)
            {
                if (sc.DisplayName.Trim() == serviceName.Trim())
                {
                    if (sc.Status.ToString() == "Running")
                    {
                        runing = true;
                    }
                }
            }
            return runing;
        }
        /// <summary>
        /// 获取本机IIS版本
        /// </summary>
        /// <returns></returns>
        public String getIISVersion() {
            String retStr = "";
            if (checkIIS())
            {
                DirectoryEntry IISService = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
                 retStr = "v"+IISService.Properties["MajorIISVersionNumber"].Value.ToString();
            }
            return retStr;
        }
 
        /// <summary>
        /// 判断程序池是否存在
        /// </summary>
        /// <param name="AppPoolName">程序池名称</param>
        /// <returns>true存在 false不存在</returns>
        private bool isAppPoolExist(string AppPoolName)
        {
            bool result = false;
            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
            foreach (DirectoryEntry appPool in appPools.Children)
            {
                if (appPool.Name.Equals(AppPoolName))
                {
                    result = true;
                }
            }
            return result;
        }
 
        /// <summary>
        /// 创建应用程序池
        /// </summary>
        /// <param name="appName">自定义引用程序池名</param>
        /// <param name="type">托管类型</param>
        /// <param name="netV">.net版本</param>
        /// <returns></returns>
        public DirectoryEntry creatAppPool(string appName, modelType type, netVersion netV)
        {
 
            if (!isAppPoolExist(appName))
            {
                DirectoryEntry newpool;
                DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
                newpool = appPools.Children.Add(appName, "IIsApplicationPool");
                //修改应用程序池配置
                setModalAndNetVersionOfappPool(appName, type, netV);
                newpool.CommitChanges();
                return newpool;
            }
            else return null;
 
        }
 
        /// <summary>
        /// 删除指定程序池
        /// </summary>
        /// <param name="AppPoolName">程序池名称</param>
        /// <returns>true删除成功 false删除失败</returns>
        private bool deleteAppPool(string AppPoolName)
        {
            bool result = false;
            if (isAppPoolExist(AppPoolName)) return result;
            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
            foreach (DirectoryEntry appPool in appPools.Children)
            {
                if (appPool.Name.Equals(AppPoolName))
                {
                    try
                    {
                        appPool.DeleteTree();
                        result = true;
                    }
                    catch
                    {
                        result = false;
                    }
                }
            }
            return result;
        }
 
        /// <summary>
        /// 设置应用程序池的托管模式和.net版本
        /// </summary>
        /// <param name="appPoolName"></param>
        /// <param name="modelType"></param>
        /// <param name="netVersion"></param>
        public void setModalAndNetVersionOfappPool(string appPoolName, modelType mt, netVersion nv) {
            if (String.IsNullOrEmpty(appPoolName)) return;
            if (isAppPoolExist(appPoolName)) return;
            if (nv == null) return;
            if (mt == null) return;
            ServerManager sm = new ServerManager();
            if (nv == netVersion.v2_0)
            {
                sm.ApplicationPools[appPoolName].ManagedRuntimeVersion = "v2.0";
            }
            else if (nv == netVersion.v4_0) {
 
                sm.ApplicationPools[appPoolName].ManagedRuntimeVersion = "v4.0";
            }
 
            if (mt == modelType.集成)
            {
                sm.ApplicationPools[appPoolName].ManagedPipelineMode = ManagedPipelineMode.Integrated;
            }
            else if(mt==modelType.经典)
            {
                sm.ApplicationPools[appPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成 Classic为经典
            }
            sm.CommitChanges();
 
        }
 
        /// <summary>
        ///检测网站名是否可用
        /// </summary>
        /// <param name="siteInfo"></param>
        /// <returns>true 可用,false 不可用过</returns>
        public bool checkWebNameIsAvailble(string webComment)
        {
            //检测网站名是否可用
            bool isAvalable = true;
            DirectoryEntry serviceEntry = new DirectoryEntry("IIS://localhost/W3SVC");
            foreach (DirectoryEntry entry in serviceEntry.Children)
            {
                if (entry.SchemaClassName == "IIsWebServer")
                {
                    string webName = entry.Properties["ServerComment"].Value.ToString();
 
                    if (webName == webComment)
                    {
                        isAvalable = false;
                        break;
                    }
                }
            }
            return isAvalable;
        }
 
        /// <summary>
        /// 检测端口号是否已被占用
        /// 注意:端口号输入有限制,在调用该方法前,需要做端口号合理性校验
        /// </summary>
        /// <param name="portNum"></param>
        /// <returns></returns>
        public bool checkPortIsVailble(string portNum) {
            bool isVailble = true;
            DirectoryEntry serviceEntry = new DirectoryEntry("IIS://localhost/W3SVC");
            foreach (DirectoryEntry entry in serviceEntry.Children)
            {
                if (entry.SchemaClassName == "IIsWebServer")
                {
                    if (entry.Properties["ServerBindings"].Value != null)
                    {
                        string Binding = entry.Properties["ServerBindings"].Value.ToString().Trim();
                        string[] Info = Binding.Split(':');
                        if (Info[1].Trim() == portNum)
                        {
                            isVailble = false;
                            break;
                        }
                    }
                }
 
            }
            return isVailble;
        }
        
        /// <summary>
        /// 创建网站
        /// </summary>
        /// <param name="siteInfo"></param>
        public DirectoryEntry creatNewWeb(newWebSiteInfo siteInfo,modelType type,netVersion netV)
        {
            
            DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC");
            int webID = 0;
            foreach (DirectoryEntry server in Services.Children)
            {
                if (server.SchemaClassName == "IIsWebServer")
                {
                    if (Convert.ToInt32(server.Name) > webID)
                    {
                        webID = Convert.ToInt32(server.Name);
                    }
                }
            }
            webID++;
 
            //创建站点
            DirectoryEntry mySitServer = Services.Children.Add(webID.ToString(), "IIsWebServer");
            mySitServer.Properties["ServerComment"].Clear();
            mySitServer.Properties["ServerComment"].Add(siteInfo.webName);
            mySitServer.Properties["Serverbindings"].Clear();
            mySitServer.Properties["Serverbindings"].Add(":" + siteInfo.porNum + ":");
            mySitServer.Properties["Path"].Clear();//注意该path为站点的路径,新增站点时,两者目录一致
            mySitServer.Properties["path"].Add(siteInfo.webPath);
            mySitServer.Properties["DefaultDoc"].Add(siteInfo.defoultPage);//设置默认文档
            
            //创建虚拟目录
            DirectoryEntry root = mySitServer.Children.Add("Root", "IIsWebVirtualDir");
            root.Properties["path"].Clear();//该路劲属性是站点下虚拟路径的路径,类似于站点的子路径
            root.Properties["path"].Add(siteInfo.visualPath);
 
            
            if (string.IsNullOrEmpty(siteInfo.appName))
            {
                root.Invoke("appCreate", 0);
            }
            else
            {
                 
                //创建引用程序池
                string appPoolName = siteInfo.appName;
                if (!isAppPoolExist(appPoolName))
                {
                    DirectoryEntry newpool;
                    DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
                    newpool = appPools.Children.Add(appPoolName, "IIsApplicationPool");
                    newpool.CommitChanges();
 
                }
                //修改应用程序池配置
                setModalAndNetVersionOfappPool(appPoolName, type,netV);
                root.Invoke("appCreate3", 0, appPoolName, true);
            }
             
            root.Properties["AppFriendlyName"].Clear();
            root.Properties["AppIsolated"].Clear();
            root.Properties["AccessFlags"].Clear();
            root.Properties["FrontPageWeb"].Clear();
            root.Properties["AppFriendlyName"].Add(root.Name);
            root.Properties["AppIsolated"].Add(2);
            root.Properties["AccessFlags"].Add(513);
            root.Properties["FrontPageWeb"].Add(1);
 
            root.CommitChanges();
            mySitServer.CommitChanges();
 
            return mySitServer;
 
        }
        /// <summary>
        /// 设置站点新增自定义mime类型,一次添加一个类型
        /// </summary>
        /// <param name="siteInfo"></param>
        /// <param name="mysiteServer"></param>
        public void addMIMEtype(newWebSiteInfo siteInfo,DirectoryEntry mysiteServer)
        {
            //需要添加新的mime类型
            if (siteInfo.newMimeType.Count > 0)
            {
                IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass();
                NewMime.Extension = siteInfo.newMimeType.Keys.ToString(); NewMime.MimeType = siteInfo.newMimeType.Values.ToString();
                IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass();
                mysiteServer.Properties["MimeMap"].Add(NewMime);
                mysiteServer.CommitChanges();
            }
 
        }
 
        /// <summary>
        /// 设置文件夹权限 处理给EVERONE赋予所有权限
        /// </summary>
        /// <param name="FileAdd">文件夹路径</param>
        public void SetFileRole(newWebSiteInfo siteInfo)
        {
            DirectoryInfo dir_info = new DirectoryInfo(siteInfo.webPath);
            DirectorySecurity dir_security = new DirectorySecurity();
            dir_security.AddAccessRule(new FileSystemAccessRule("Everyone ", FileSystemRights.WriteData, AccessControlType.Allow));
            dir_info.SetAccessControl(dir_security);
        }
 
        /// <summary>
        /// 删除网站
        /// </summary>
        /// <param name="webName"></param>
        public void deletWeb(string webName) {
            DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC");
            foreach (DirectoryEntry server in Services.Children)
            {
                if (server.SchemaClassName == "IIsWebServer")
                {
                    if (server.Properties["ServerComment"].Value == webName || server.Name==webName)
                    {
                        server.DeleteTree();
                        break;
                    }
                }
            }
 
            Services.CommitChanges();
        }
    }
}

C#操作IIS服务的更多相关文章

  1. bat文件重启SQL服务和IIS服务

    sqlserver有自动备份功能,所以要重启两个服务器,下面是重启脚本,脚本名称:sql_restart.bat    net stop sqlserveragent net stop mssqlse ...

  2. WCF技术剖析之三:如何进行基于非HTTP的IIS服务寄宿

    原文:[原创]WCF技术剖析之三:如何进行基于非HTTP的IIS服务寄宿 在上面一篇文章中,我们对不同版本的IIS,以及ASP.NET得的实现机制进行了详细而深入的分析.在介绍IIS7.0的时候,我们 ...

  3. C#操作IIS完整解析

    原文:C#操作IIS完整解析 最近在为公司实施做了一个工具,Silverlight部署早已是轻车熟路, 但对于非技术人员来说却很是头疼的一件事,当到现场实施碰到客户情况也各不相同, 急需一个类似系统备 ...

  4. 最完整的dos命令字典,IIS服务命令,FTP命令

    https://www.cnblogs.com/accumulater/p/10670051.html(优秀博文) 一.最完整的dos命令字典net use ipipc$ " " ...

  5. C# 在程序中控制IIS服务或应用程序池关闭重启

    //停止IIS服务 ServiceController sc = new ServiceController("iisadmin"); if(sc.Status=ServiceCo ...

  6. 完整安装IIS服务

    此文主要是针对前面提到的 IIS支持json.geojson文件 添加脚本映射时,提示找不到asp.dll时的解决方法. 主要参考了此文:http://www.kodyaz.com/articles/ ...

  7. 在vmware虚拟机下的Window2003服务器下安装IIS服务详细教程——超级详细(解决关于:800a0bb9的解决办法)

    总的来说,就是9步: 1.控制面板添加或者删除程序2.删除想要删的3.打开IIS配置4.开始共享5.导入源码6.配置权限7.网站属性.文档.应用程序配置8.web服务扩展9.访问网站 在安装好虚拟机的 ...

  8. C#操作注册服务卸载服务启动服务停止服务.. .

    using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; usi ...

  9. Windows 远程停止iis服务

    最近遇到一个小需求,需要重启远程计算机的iis服务. 需求背景是这样的,用jenkins 做ci的时候, 由于项目是有单独的web服务器,项目虽然是一套, 但是分为A,B,C三个web系统,其中A,B ...

随机推荐

  1. (转)RabbitMQ学习之消息可靠性及特性

    http://blog.csdn.net/zhu_tianwei/article/details/53971296 下面主要从队列.消息发送.消息接收方面了解消息传递过的一些可靠性处理. 1.队列 消 ...

  2. mac安装win10后触摸板没有右键功能键的添加技巧

    一些mac用户也会在自己的笔记本电脑上安装windows10系统. 但最近有部分用户发现,安装上win10正式版后,发现无论点击触摸板哪个位置,都只有左键,根本无法右键的问题, 针对此问题,现笔者分享 ...

  3. java 监听控制台输入

    分享一下我写的java监听控制台输入并可以给出响应的功能. 很多时候需要监听控制台的输入内容,相当于信号监听,根据输入的内容做出相应的动作,这里给出我的一个简单实现. 要注意的是:监听得到的消息中前后 ...

  4. SQLServer Oracle MySQL的区别

    table tr:nth-child(odd){ background: #FFFFCC; font-size: 18px; } table tr:nth-child(even){ backgroun ...

  5. js操作table中tr的顺序,实现上移下移一行的效果

    总体思路是在table外部加个div,修改div的innerHtml实现改变tr顺序的效果 具体思路是 获取当前要移动tr行的rowIndex,在table中删除掉,然后循环table的rows,到了 ...

  6. ansible --help 文档

    > ansible --help Usage: ansible <host-pattern> [options] Define and run a single task 'play ...

  7. VLC编译

    http://blog.csdn.net/hdh4638/article/details/7602321 1 下载代码 ki.videolan.org/VLC_Source_code git colo ...

  8. socketserver模块初识

    python提供了两个级别访问的网络服务: 低级的网络服务支持基本的socket,它提供了标准的BSD sockets API,可以访问底层操作系统socket接口的全部方法 高级别的网络服务模块so ...

  9. HDU 5172

    超内存了,呃...不知道如何优化了. 首先要判断区间的和是否和1~n的和相等. 再个,记录下每个数字前一次出现的位置,求这些位置的最大值,如果小于左端点,则表示有这样的一个序列. 呃~~~第二个条件当 ...

  10. POJ 1777

    一道好题. 由算术基本定理,知: 那么,对于上式的每个因子值只能是2^M的形式.取第一个式子为例,通过分解因式出(1+p^2)=2^k知,a只能为1. 于是对于p只能是梅森素数.而且每个梅森素数只能出 ...