. Net restart iis thread pool and iis Site program code sharing

Source: Internet
Author: User

Restart site:

Copy codeThe Code is as follows:
/// <Summary>
/// Restart the Site Based on the name. (The thread pool is not restarted)
/// </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 ("cocould not stop website! ");
Throw new InvalidOperationException ("cocould not stop website! ");
}
Site. Start ();
}
Else
{
Console. WriteLine ("cocould not find website! ");

Throw new InvalidOperationException ("cocould not find website! ");
}

}
Catch (Exception e)
{
Console. WriteLine (e );
}
}

/// <Summary>
/// After the restart, check whether it is enabled.
/// </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 status from being Enabled
If (site. State! = ObjectState. Started)
{
Site. Start ();
}
}
}
}
Catch (Exception e)
{
Console. WriteLine (e );
}
}

Restart the iis thread pool:

Copy codeThe Code is 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 the 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>
/// Close the 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 terminate the process when the command for sending and closing the window is invalid.
Result = true;
}
}
Return result;
}

Related Article

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.