.net重啟iis線程池和iis站點程序代碼分享
重啟站點:
/// <summary>
/// 根據(jù)名字重啟站點.(沒重啟線程池)
/// </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);
//防止狀態(tài)為正在開啟
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>
/// 關(guān)閉進程
/// </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(); // 當發(fā)送關(guān)閉窗口命令無效時強行結(jié)束進程
result = true;
}
}
return result;
}
相關(guān)文章
asp.net運行提示未將對象引用設(shè)置到對象的實例錯誤解決方法
asp.net運行提示未將對象引用設(shè)置到對象的實例錯誤解決方法,需要的朋友可以參考下2012-03-03
使用Blazor框架實現(xiàn)在前端瀏覽器中導(dǎo)入和導(dǎo)出Excel
Blazor?是一個相對較新的框架,用于構(gòu)建具有?.NET?強大功能的交互式客戶端?Web?UI,本文主要介紹了如何在?Blazor?應(yīng)用程序中實現(xiàn)?SpreadJS?利用?.NET?的強大功能完成瀏覽器端的?Excel?導(dǎo)入導(dǎo)出,需要的可以參考一下2023-05-05
.NET CORE動態(tài)調(diào)用泛型方法詳解
這篇文章主要為大家詳細介紹了.NET CORE動態(tài)調(diào)用泛型方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
ASP.NET MVC懶加載如何逐步加載數(shù)據(jù)庫信息
在ASP.NET MVC中實現(xiàn)數(shù)據(jù)庫的逐步加載可通過懶加載技術(shù)完成,首先,在EntityFramework中配置數(shù)據(jù)庫上下文,使用對應(yīng)的實體類映射數(shù)據(jù)庫表,本文給大家介紹ASP.NET MVC懶加載如何逐步加載數(shù)據(jù)庫信息,感興趣的朋友跟隨小編一起看看吧2024-10-10
asp.net+jquery ajax無刷新登錄的實現(xiàn)方法
asp.net+jquery ajax無刷新登錄的實現(xiàn)方法,需要的朋友可以參考一下2013-06-06
解析GridView自帶分頁及與DropDownList結(jié)合使用
本文主要介紹了GridView自帶的分頁功能的實現(xiàn)方法。具有一定的參考價值,需要的朋友一起來看下吧2016-12-12
asp.net Repeater取得CheckBox選中的某行某個值的c#寫法
asp.net(c#)利用Repeater取得CheckBox選中行的某個值的代碼2008-08-08

