您好,登錄后才能下訂單哦!
本篇內容主要講解“如何編寫.net重啟iis線程池和iis站點程序代碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何編寫.net重啟iis線程池和iis站點程序代碼”吧!
重啟站點:
復制代碼 代碼如下:
/// <summary>
/// 根據名字重啟站點.(沒重啟線程池)
/// </summary>
/// <param name="sitename"></param>
static void RestartWEbSite(string sitename)
{
try
{
var server = new ServerManager();
var site = server.Sites.FirstOrDefault(s => s.Name == sitename);
if (site != null)
{
site.Stop();
if (site.State == ObjectState.Stopped)
{
}
else
{
Console.WriteLine("Could not stop website!");
throw new InvalidOperationException("Could not stop website!");
}
site.Start();
}
else
{
Console.WriteLine("Could not find website!");
throw new InvalidOperationException("Could not find website!");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
/// <summary>
/// 重啟完之后.要再檢測下.是否開啟了
/// </summary>
/// <param name="sitename"></param>
static void FixWebsite(string sitename)
{
try
{
var server = new ServerManager();
var site = server.Sites.FirstOrDefault(s => s.Name == sitename);
if (site != null)
{
if (site.State != ObjectState.Started)
{
Thread.Sleep(500);
//防止狀態為正在開啟
if (site.State != ObjectState.Started)
{
site.Start();
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
重啟iis線程池:
復制代碼 代碼如下:
/// <summary>
/// 線程池名字
/// </summary>
/// <param name="name"></param>
static void RestartIISPool(string name)
{
string[] cmds = { "c:", @"cd %windir%\system32\inetsrv", string.Format("appcmd stop apppool /apppool.name:{0}", name), string.Format("appcmd start apppool /apppool.name:{0}", name) };
Cmd(cmds);
CloseProcess("cmd.exe");
}
/// <summary>
/// 運行CMD命令
/// </summary>
/// <param name="cmd">命令</param>
/// <returns></returns>
public static string Cmd(string[] cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i++)
{
p.StandardInput.WriteLine(cmd[i]);
}
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
//Debug.Print(strRst);
p.WaitForExit();
p.Close();
return strRst;
}
/// <summary>
/// 關閉進程
/// </summary>
/// <param name="ProcName">進程名稱</param>
/// <returns></returns>
public static bool CloseProcess(string ProcName)
{
bool result = false;
var procList = new ArrayList();
foreach (Process thisProc in Process.GetProcesses())
{
var tempName = thisProc.ToString();
int begpos = tempName.IndexOf("(") + 1;
int endpos = tempName.IndexOf(")");
tempName = tempName.Substring(begpos, endpos - begpos);
procList.Add(tempName);
if (tempName == ProcName)
{
if (!thisProc.CloseMainWindow())
thisProc.Kill(); // 當發送關閉窗口命令無效時強行結束進程
result = true;
}
}
return result;
}
到此,相信大家對“如何編寫.net重啟iis線程池和iis站點程序代碼”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。