C# 重启IIS站点及应用程序池

1、通过线程操作

    static void Main(string[] args)
	{
		Console.WriteLine("检测程序启动运行,自动启动或停止网站及应用池。");
		new Thread(RecoveryWebSite).Start();//开启线程
	}
	/// <summary>
	/// 停止/启动iis站点
    /// </summary>
    public static void RecoveryWebSite()
    {
    	const string AppPoolName = "";//iis应用程序池名称
		const string WebSiteName = "";//iis网站名称
		const int SleepTime = 1000 * 30;//间隔事件:每30秒执行一次
		ServerManager sm = new ServerManager();
		while (true)
		{
			try
			{
				var pool = sm.ApplicationPools[AppPoolName];//根据应用程序池名称获取
				if (pool != null)
				{
					if (pool.State == ObjectState.Stopped)
					{
						WriteLog("检测到应用池" + AppPoolName + "停止服务");
						WriteLog("正在启动应用池" + AppPoolName);
						if (pool.Start() == ObjectState.Started)
						{
							WriteLog("成功启动应用池" + AppPoolName);
						}
						else
                        {
							WriteLog("启动应用池" + AppPoolName + "失败. " + SleepTime / 60 + "秒后重试启动");
						}
					}
					else if (pool.State == ObjectState.Started)
					{
						if (pool.Stop() == ObjectState.Stopped)
						{
                        	WriteLog("成功停止应用池" + AppPoolName);
                        }
                        else
	                    {
                        	WriteLog("停止应用池" + AppPoolName + "失败. " + SleepTime / 60 + "秒后重试启动");
                        }
					}
				}
				Site site = sm.Sites[WebSiteName];//根据网站名称获取IIS站点
				if (site != null)
				{
					if (site.State == ObjectState.Stopped)
					{
						WriteLog("检测到网站" + WebSiteName + "停止服务");
						WriteLog("正在启动网站" + WebSiteName);
						if (site.Start() == ObjectState.Started)
						{
							WriteLog("成功启动网站" + WebSiteName);
						}
						else
						{
							WriteLog("启动网站" + WebSiteName + "失败. " + SleepTime / 60 + "秒后重试启动");
						}
					}
					else if (site.State == ObjectState.Started)
					{
						if (site.Stop() == ObjectState.Stopped)
						{
							WriteLog("成功停止网站" + AppPoolName);
						}
						else
						{
							WriteLog("停止网站" + AppPoolName + "失败. " + SleepTime / 60 + "秒后重试启动");
						}
					}
				}
			}
			catch (Exception ex)
			{
				WriteLog(ex.Message.ToString());
			}
			GC.Collect();
			Thread.Sleep(SleepTime);//暂停运行
		}
	}  
	/// <summary>
	/// 写日志
	/// </summary>
	/// <param name="msg"></param>
	public static void WriteLog(string msg)
	{
		var fPath = "C:\\Users\\Administrator\\Desktop\\更新日志.txt";
		if (!File.Exists(fPath))
		{
			File.Create(fPath).Close();
		}
		using (StreamWriter sw = new StreamWriter(fPath, true, Encoding.UTF8))
		{
			sw.WriteLine(string.Format("{0} , 时间{1}", msg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
		}
	} 

2、通过定时器操作

/// <summary>
/// IIS访问对象
/// </summary>
static ServerManager serverManager;
/// <summary>
/// 获取Config对象
/// </summary>
static System.Configuration.Configuration config;
/// <summary>
/// 间隔时间
/// </summary>
static int SleepTime;
/// <summary>
/// 定时器
/// </summary>
static System.Timers.Timer timer;
/// <summary>
/// 主程序入口
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
	Console.WriteLine("检测程序启动运行,自动启动或停止网站及应用池。");
	serverManager = new ServerManager();
	config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
	SleepTime = int.Parse(config.AppSettings.Settings["SleepTime"].Value);

	timer timer = new System.Timers.Timer();
	timer.Interval = SleepTime;//设置间隔时间
	timer.Elapsed += new System.Timers.ElapsedEventHandler(RecoveryWebSite);
	timer.Enabled = true;
	timer.AutoReset = true;//保持一直运行
	timer.Start();
	while (true) { }//死循环,与 Console.ReadKey();效果类似,但去除了键盘输入导致关闭风险
}
/// <summary>
/// 停止/启动iis站点
/// </summary>
public static void RecoveryWebSite(object sender, System.Timers.ElapsedEventArgs e)
{
    //定时任务方法明细	
}  
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值