简体   繁体   中英

How can I check if my IIS is alive using C#?

How can I check if my IIS is alive using C#?

If the server is down - how to do iisreset?

How about using WebRequest to try opening a page? If it doesn't return anything to you then perhaps use the Process class to call iisreset.

// Initialise the WebRequest.
WebRequest webRequest = WebRequest.Create("[your URI here]");
// Return the response. 
WebResponse webResponse = webRequest.GetResponse();
// Close the response to free resources.
webResponse.Close();

if (webResponse.ContentLength > 0) // May have to catch an exception here instead
{
    Process.Start("iisreset.exe", "/reset"); // Or whatever arg you want

}

It needs finessing but this is the broad outline of what you asked for...

You can create a new WebRequest to localhost. If you get a response, that means your IIS is up, if not, it's down.

To reset it, create a new Process and pass iisreset as an argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM