Link to home
Create AccountLog in
Avatar of Bytech India
Bytech India

asked on

Need to check the internet connectivity in C#

Hello all

I need to check the internet connectivity in C# to display online and offline.
I have used the following call but they give wrong results in some situation.
(1) private bool IsOnline()
        {
            try
            {
                IPHostEntry dummy = Dns.GetHostEntry("www.google.com"); //using System.Net;
                return true;
            }
            catch (SocketException ex)
            {
                return false;
            }
        }

(2) if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                MessageBox.Show("There may be a problem in your internet connection. Please try again later", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

Please provide me a call which will work only when internet will be working.
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Bytech India
Bytech India

ASKER

Thank you