Restart site:
Copy Code code as follows:
<summary>
Restart the site by name. (no restart thread pool)
</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>
After the reboot. To be tested again. Is it open?
</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);
Prevent the state from being turned on
if (site. State!= objectstate.started)
{
Site. Start ();
}
}
}
}
catch (Exception e)
{
Console.WriteLine (e);
}
}
To restart the IIS thread pool:
Copy Code code as follows:
<summary>
Thread pool Name
</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>
Run cmd command
</summary>
<param name= "cmd" > Command </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>
Shutdown process
</summary>
<param name= "procname" > Process name </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 (); Forcibly end a process when the Send close window command is invalid
result = true;
}
}
return result;
}