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