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);
}
}