I've created a simple method to check if my Internet connection is active. Unfortunately, the try section becomes overlooked because of System.Net.NetworkInformation.PingException” in System.Net.Ping.dll. As well noticed, the problem occurs with PingReply. Could you tell me how to deal with this excepion and allow code to run correctly so to execute try section.
MyFunction:
private void ConnectionStatusChecker(object source, ElapsedEventArgs e)
{
Ping myPing = new Ping();
String host = "https://www.google.com/";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
Ping ping = new Ping();
try
{
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success)
ConnectionStatus = "Connected";
else
ConnectionStatus = "Disconnected";
}
catch (System.Net.NetworkInformation.PingException)
{
ConnectionStatus = "Disconnected-Exception";
}
}
Edit: I would like to store in ConnectionStatus Variable the information about my Internet Connection. Try section describes the value in case of beeing disconnected or connected. But PingException does not allow me to check it - exception automatically blocks try section. The thing is, how to deal with this exception, where is the source or mistake I made?