. NET restart the IIS thread pool and the IIS Site program code sharing _ Practical Tips

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.