Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write. Net restart iis thread pool and iis site program code

Shulou Source: shulou.com Published: 2022-06-03 07:41:33 09月15日 Update

本篇内容主要讲解"如何编写.net重启iis线程池和iis站点程序代码",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何编写.net重启iis线程池和iis站点程序代码"吧!

重启站点:

复制代码 代码如下:

///

/// 根据名字重启站点.(没重启线程池)

///

///

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

}

}

///

/// 重启完之后.要再检测下.是否开启了

///

///

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线程池:

复制代码 代码如下:

///

/// 线程池名字

///

///

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

}

///

/// 运行CMD命令

///

/// 命令

///

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;

}

///

/// 关闭进程

///

/// 进程名称

///

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站点程序代码"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Tags: Code thread site program command process content name learning utility deeper interest name utility practical simple operation method more friends are Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Apple Shulou Tech Info OPPO Reno