http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 9855|回复: 2

[功能帮助类] C# 操作IIS加强版(添加,删除,启动,暂停网站,默认页,绑定信息)

[复制链接]
发表于 2019-4-11 11:33:34 | 显示全部楼层 |阅读模式
C# 操作IIS加强版(添加,删除,启动,暂停网站,默认页,绑定信息)

主要功能如下
  • 在本机的IIS创建Web网站
  • 删除网站包括应用程序池
  • 删除应用程序池
  • 添加默认文档
  • 删除默认文档
  • 添加虚拟目录
  • 删除虚拟目录
  • 启动网站
  • 暂停网站
  • 根据网站名获取网站信息
  • 获取所有网站的的网站数据
  • 添加绑定信息(IP,端口,域名)
  • 删除绑定信息


我直接贴类,里面注解很详细大家自己看一下吧。
[C#] 纯文本查看 复制代码
using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoIIS.Helper
{
    /// <summary>
    /// IIS操作类
    /// </summary>
    public class IISHelper
    {
        #region 创建网站
        /// <summary> 
        /// 在本机的IIS创建Web网站
        /// </summary> 
        /// <param name="siteName">网站名</param> 
        /// <param name="bindingInfo">host <example>"*:80:myhost.com"</example></param> 
        /// <param name="physicalPath">网站路径</param> 
        /// <returns>成功返回True,失败返回false</returns>     
        public static bool CreateSite(string siteName, string bindingInfo, string physicalPath)
        {
            return CreateSite(siteName, "http", bindingInfo, physicalPath, true, siteName, ProcessModelIdentityType.ApplicationPoolIdentity, null, null, ManagedPipelineMode.Integrated, null);
        }
        /// <summary>
        /// 在本机的IIS创建Web网站
        /// </summary>
        /// <param name="siteName">网站名</param>
        /// <param name="protocol">http头</param>
        /// <param name="bindingInformation">网站例如<example>"*:80:[url=http://www.sufeinet.com]www.sufeinet.com[/url]"</example></param>
        /// <param name="physicalPath">网站的路径</param>
        /// <param name="createAppPool">是否创建应用程序程序池</param>
        /// <param name="appPoolName">应用程序名</param>
        /// <param name="identityType">标识</param>
        /// <param name="appPoolUserName">用户名没有时用户名为Null即可</param>
        /// <param name="appPoolPassword">密码</param>
        /// <param name="appPoolPipelineMode">模式,经典还是集成</param>
        /// <param name="managedRuntimeVersion">.net版本</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool CreateSite(string siteName, string protocol, string bindingInformation, string physicalPath, bool createAppPool, string appPoolName,
            ProcessModelIdentityType identityType, string appPoolUserName, string appPoolPassword, ManagedPipelineMode appPoolPipelineMode, string managedRuntimeVersion)
        {
            using (ServerManager mgr = new ServerManager())
            {
                //删除网站和应用程序池
                DeleteSite(siteName);
                Site site = mgr.Sites.Add(siteName, protocol, bindingInformation.ToLower(), physicalPath);

                // PROVISION APPPOOL IF NEEDED 
                if (createAppPool)
                {
                    ApplicationPool pool = mgr.ApplicationPools.Add(appPoolName);
                    if (pool.ProcessModel.IdentityType != identityType)
                    {
                        pool.ProcessModel.IdentityType = identityType;
                    }
                    if (!String.IsNullOrEmpty(appPoolUserName))
                    {
                        pool.ProcessModel.UserName = appPoolUserName;
                        pool.ProcessModel.Password = appPoolPassword;
                    }
                    if (appPoolPipelineMode != pool.ManagedPipelineMode)
                    {
                        pool.ManagedPipelineMode = appPoolPipelineMode;
                    }
                    site.Applications["/"].ApplicationPoolName = pool.Name;
                }
                if (site != null)
                {
                    mgr.CommitChanges();
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region 删除网站和应用程序池
        /// <summary> 
        /// 删除网站包括应用程序池
        /// </summary> 
        /// <param name="siteName">网站名</param> 
        /// <param name="isAppPool">是否删除应用程序池默认为删除</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool DeleteSite(string siteName, Boolean isAppPool = true)
        {
            using (ServerManager mgr = new ServerManager())
            {
                //判断web应用程序是否存在
                if (mgr.Sites[siteName] != null)
                {
                    if (isAppPool)
                    {
                        //判断应用程序池是否存在
                        if (mgr.ApplicationPools[siteName] != null)
                        {
                            mgr.ApplicationPools.Remove(mgr.ApplicationPools[siteName]);
                        }
                    }
                    mgr.Sites.Remove(mgr.Sites[siteName]);
                    mgr.CommitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary> 
        /// 删除应用程序池
        /// </summary> 
        /// <param name="appPoolName">应用程序池名</param> 
        /// <returns>成功返回True,失败返回false</returns>
        public static bool DeletePool(string appPoolName)
        {
            using (ServerManager mgr = new ServerManager())
            {
                ApplicationPool pool = mgr.ApplicationPools[appPoolName];
                if (pool != null)
                {
                    mgr.ApplicationPools.Remove(pool);
                    mgr.CommitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region 默认文档 添加和删除
        /// <summary>
        /// 添加默认文档
        /// </summary>
        /// <param name="siteName">网站</param>
        /// <param name="defaultDocName">默认文档名</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool AddDefaultDocument(string siteName, string defaultDocName)
        {
            using (ServerManager mgr = new ServerManager())
            {
                Configuration cfg = mgr.GetWebConfiguration(siteName);
                ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
                ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
                ConfigurationElementCollection filesCollection = filesElement.GetCollection();

                foreach (ConfigurationElement elt in filesCollection)
                {
                    if (elt.Attributes["value"].Value.ToString() == defaultDocName)
                    {
                        return false;//添加时存在
                    }
                }
                try
                {
                    //创建一个新的默认页
                    ConfigurationElement docElement = filesCollection.CreateElement();
                    docElement.SetAttributeValue("value", defaultDocName);
                    filesCollection.Add(docElement);
                }
                catch (Exception)
                {
                    return false;//添加时发生错误 
                }

                mgr.CommitChanges();
            }
            return true;//添加成功
        }
        /// <summary>
        /// 删除默认文档
        /// </summary>
        /// <param name="siteName">网站</param>
        /// <param name="defaultDocName">默认文档名</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool DeleteDefaultDocument(string siteName, string defaultDocName)
        {
            using (ServerManager mgr = new ServerManager())
            {
                Configuration cfg = mgr.GetWebConfiguration(siteName);
                ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
                ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
                ConfigurationElementCollection filesCollection = filesElement.GetCollection();

                //创建一个新的默认页
                ConfigurationElement docElement = filesCollection.CreateElement();

                bool isdefault = false;
                //不存在则返回
                foreach (ConfigurationElement elt in filesCollection)
                {
                    if (elt.Attributes["value"].Value.ToString() == defaultDocName)
                    {
                        docElement = elt;
                        isdefault = true;
                    }
                }
                if (!isdefault)
                {
                    return false;//不存在
                }
                try
                {
                    filesCollection.Remove(docElement);
                }
                catch (Exception)
                {
                    return false;//删除时发生错误 
                }

                mgr.CommitChanges();
            }
            return true;//删除成功
        }
        #endregion

        #region 虚拟目录添加和删除

        /// <summary>
        /// 添加虚拟目录
        /// </summary>
        /// <param name="siteName">网站名</param>
        /// <param name="vDirName">目录名</param>
        /// <param name="physicalPath">对应的文件夹路径</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool CreateVDir(string siteName, string vDirName, string physicalPath)
        {
            using (ServerManager mgr = new ServerManager())
            {
                Site site = mgr.Sites[siteName];
                if (site == null)
                {
                    return false;
                }
                site.Applications.Add("/" + vDirName, physicalPath);
                mgr.CommitChanges();
            }
            return true;
        }
        /// <summary>
        /// 删除虚拟目录
        /// </summary>
        /// <param name="siteName">网站名</param>
        /// <param name="vDirName">目录名</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool DeleteVDir(string siteName, string vDirName)
        {
            using (ServerManager mgr = new ServerManager())
            {
                Site site = mgr.Sites[siteName];
                if (site == null)
                {
                    return false;
                }
                site.Applications.Remove(site.Applications["/" + vDirName]);
                mgr.CommitChanges();
            }
            return true;
        }
        #endregion

        #region 网站重启,暂停,启动
        /// <summary>
        /// 启动网站
        /// </summary>
        /// <param name="siteName">网站名</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool Start(string siteName)
        {
            try
            {
                using (ServerManager mgr = new ServerManager())
                {
                    if (mgr.Sites[siteName].State == ObjectState.Stopped)
                    {
                        mgr.Sites[siteName].Start();
                    }
                    mgr.CommitChanges();
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 暂停网站
        /// </summary>
        /// <param name="siteName">网站名</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool Stop(string siteName)
        {
            try
            {
                using (ServerManager mgr = new ServerManager())
                {
                    if (mgr.Sites[siteName].State == ObjectState.Started)
                    {
                        mgr.Sites[siteName].Stop();
                    }
                    mgr.CommitChanges();
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region 获取网站的信息
        /// <summary>
        /// 根据网站名获取网站信息
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public static Site GetSiteInfo(string siteName)
        {
            using (ServerManager mgr = new ServerManager())
            {
                return mgr.Sites[siteName];
            }
        }
        /// <summary>
        /// 获取所有的网站数据
        /// </summary>
        /// <returns>SiteCollection</returns>
        public static SiteCollection GetSiteList()
        {
            using (ServerManager mgr = new ServerManager())
            {
                return mgr.Sites;
            }
        }
        #endregion
        #region 添加绑定信息
        /// <summary>
        /// 添加绑定信息
        /// </summary>
        /// <param name="siteName">网站名</param>
        /// <param name="ip">ip</param>
        /// <param name="port">port</param>
        /// <param name="domain">domain</param>
        /// <param name="bindingProtocol">协议头如 http/https默认为http</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool AddHostBinding(string siteName, string ip, string port, string domain, string bindingProtocol = "http")
        {
            try
            {
                using (ServerManager mgr = new ServerManager())
                {
                    string binginfo = string.Format("{0}:{1}:{2}", ip, port, domain).ToLower();
                    BindingCollection binding = mgr.Sites[siteName].Bindings;
                    binding.Add(binginfo, bindingProtocol);
                    mgr.CommitChanges();
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 删除绑定信息
        /// </summary>
        /// <param name="siteName">网站名</param>
        /// <param name="ip">ip</param>
        /// <param name="port">port</param>
        /// <param name="domain">domain</param>
        /// <returns>成功返回True,失败返回false</returns>
        public static bool DeleteHostBinding(string siteName, string ip, string port, string domain)
        {
            try
            {
                using (ServerManager mgr = new ServerManager())
                {
                    string binginfo = string.Format("{0}:{1}:{2}", ip, port, domain).ToLower();
                    foreach (Binding item in mgr.Sites[siteName].Bindings)
                    {
                        if (item.BindingInformation == binginfo)
                        {
                            mgr.Sites[siteName].Bindings.Remove(item);
                            break;
                        }
                    }
                    mgr.CommitChanges();
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        #endregion
    }
}


免费拿 去不谢

调取代码
[C#] 纯文本查看 复制代码
using AutoIIS.Helper;
using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoIIS
{
    public partial class Index : Form
    {
        public Index()
        {
            InitializeComponent();
        }

        private void Index_Load(object sender, EventArgs e)
        {

        }
        //添加默认页
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.AddDefaultDocument("苏飞论坛", txtdefault.Text.Trim());
        }

        //创建网站
        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.CreateSite(txtsite.Text.Trim(), txtdomain.Text.Trim(), txtfile.Text.Trim());
        }
        //删除默认文档
        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.DeleteDefaultDocument("苏飞论坛", txtdefault.Text.Trim());
        }
        //删除网站
        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.DeleteSite(txtsite.Text.Trim());
        }
        //删除
        private void button6_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.DeleteVDir(txtsite.Text.Trim(), txtxuniname.Text.Trim());
        }
        //添加
        private void button5_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.CreateVDir(txtsite.Text.Trim(), txtxuniname.Text.Trim(), txtxunipath.Text.Trim());
        }
        //启动
        private void button7_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.Start(txtsite.Text.Trim());
        }
        //暂停
        private void button8_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += IISHelper.Stop(txtsite.Text.Trim());
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Site site = IISHelper.GetSiteInfo(txtsite.Text.Trim());
            richTextBox1.Text += "网站名:" + site.Name + "\r\n";
            richTextBox1.Text += "网站状态:" + site.State.ToString() + "\r\n";
            foreach (var item in site.Bindings)
            {
                richTextBox1.Text += "host:" + item.Host + "\r\n";
                richTextBox1.Text += "bind:" + item.BindingInformation.ToString() + "\r\n";
            }

            richTextBox1.Text += "网站状态:" + site.Applications.ToString().ToString() + "\r\n";
        }
        //添加
        private void button10_Click(object sender, EventArgs e)
        {
            string[] strlist = txtdomain.Text.Trim().Split(':');
            richTextBox1.Text += IISHelper.AddHostBinding(txtsite.Text.Trim(), strlist[0], strlist[1], strlist[2]);
        }
        //删除
        private void button11_Click(object sender, EventArgs e)
        {
            string[] strlist = txtdomain.Text.Trim().Split(':');
            richTextBox1.Text += IISHelper.DeleteHostBinding(txtsite.Text.Trim(), strlist[0], strlist[1], strlist[2]);
        }
    }
}





1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2019-4-11 11:37:25 | 显示全部楼层
需要添加引用
Microsoft.Web.Administration
发表于 2019-4-12 15:48:43 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-22 06:39

© 2014-2021

快速回复 返回顶部 返回列表