Gali
Gali

Reputation: 14953

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 ?

Upvotes: 0

Views: 570

Answers (2)

noonand
noonand

Reputation: 2855

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...

Upvotes: 2

Tushar
Tushar

Reputation: 1252

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.

Upvotes: 1

Related Questions