Process p = new Process(); p.StartInfo = new ProcessStartInfo { FileName = @"c:\windows\system32\inetsrv\AppCmd.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是否有變化來判斷是否重啟成功,重啟邏輯應該是先新增一個,再刪除舊的,剛回收時,會同時存在兩個進程。