Process p = new Process(); p.StartInfo = new ProcessStartInfo { FileName = @"c:windowssystem32inetsrvAppCmd.exe", Arguments = "recycle apppool /apppool.name:xxx", CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = false, RedirectStandardError = false }; p.EnableRaisingEvents = true; p.Start(); p.WaitForExit();
或者下面方法
using Microsoft.Web.Administration;
var result = ""; ServerManager sm = new ServerManager(); var pool = sm.ApplicationPools["xxx"]; if (pool != null && pool.State == ObjectState.Stopped) { if (pool.Start() == ObjectState.Started) { result += "start ok!"; } } if(pool!=null && pool.State == ObjectState.Started) { if (pool.Recycle() == ObjectState.Started) { result += "recyle ok!"; } }
下面的方法,在无权限的时候,可以试试
进程里看w3wp对应的id是否有变化来判断是否重启成功,重启逻辑应该是先新增一个,再删除旧的,刚回收时,会同时存在两个进程。