明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

C#重启IIS指定网站和指定应用程序池

Posted on   且行且思  阅读(376)  评论(0编辑  收藏  举报
复制代码
using System;
using System.Net;
using System.IO;

using System.Timers;
using System.Threading;
using System.Diagnostics;
using System.Xml;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using System.ServiceProcess;
using Microsoft.Web.Administration; //添加程序集>扩展>引用
复制代码

 

复制代码
 /// <summary>
        /// 开始执行设置IIS站点和程序池
        /// </summary>
        /// <param name="Task_Channel">操作渠道</param>
        public void SetIIS()
        {
            var sm = new ServerManager();
            try
            {               
                Log.WriteTextLog("SetIIS.Service", "SetIIS", "SetIIS");

                string str = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                string AppPoolName = "SFSAppPool";      //程序池名称
                string WebSiteName = "Default";        //iis 注意是父级 站点名称               

                var pool = sm.ApplicationPools[AppPoolName];
                if (pool != null)
                {
                    if (pool.State == ObjectState.Started)
                    {
                        pool.Stop();
                        Log.WriteTextLog("SetIIS.Service", "ApplicationPools", "检测到应用池" + AppPoolName + "停止服务");
                        Thread.Sleep(3000);
                    }
                    if (pool.State == ObjectState.Stopped)
                    {
                        pool.Start();
                        Log.WriteTextLog("SetIIS.Service", "ApplicationPools", "正在启动应用池" + AppPoolName);
                        Thread.Sleep(3000);
                    }
                    if (pool.Start() == ObjectState.Started)
                    {
                        Log.WriteTextLog("SetIIS.Service", "ApplicationPools", "成功启动应用池" + AppPoolName);
                    }
                    else
                    {                        
                        Log.WriteTextLog("SetIIS.Service", "ApplicationPools", "启动应用池" + AppPoolName + "失败");
                        Thread.Sleep(3000);
                        pool.Start();
                    }
                    Thread.Sleep(50);
                }

                var site = sm.Sites[WebSiteName];
                if (site != null)
                {
                    if (site.State == ObjectState.Started)
                    {
                        site.Stop();
                        Log.WriteTextLog("SetIIS.Service", "Sites", "检测到网站" + WebSiteName + "停止服务");
                        Thread.Sleep(3000);
                    }
                    if (site.State == ObjectState.Stopped)
                    {
                        site.Start();
                        Log.WriteTextLog("SetIIS.Service", "Sites", "正在启动网站" + WebSiteName);
                        Thread.Sleep(3000);
                    }
                    if (site.Start() == ObjectState.Started)
                    {
                        Log.WriteTextLog("SetIIS.Service", "Sites", "成功启动网站" + WebSiteName);
                    }
                    else
                    {

                        Log.WriteTextLog("SetIIS.Service", "Sites", "启动网站" + WebSiteName + "失败");
                        Thread.Sleep(3000);
                        site.Start();
                    }
                    Thread.Sleep(50);
                }

            }
            catch (WebException ex)
            {
                string stacktrace = ex.StackTrace;//获得详细的错误位置    

                Log.WriteTextLog("SetIIS.Service.Error", "WebException", stacktrace + "##" + ex.Message);
            }
            finally
            {
                if(sm != null) { sm.Dispose(); }
                if(sm != null) { sm = null; }

                //回收内存  
                Thread.Sleep(16);
            }
        }
复制代码

 

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示