Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check the internet connectivity thro c#.My computer is connected with Network only internet is disabled.How do i find the internet connectivity status.
The following code is not suitable for my returns true even only network is connected and internet disabled.

  private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );
  public static bool IsConnectedToInternet( )
    int Desc ;
    return InternetGetConnectedState( out Desc, 0 ) ;

This(following) code works but takes more time and makes my application hangs,
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("");
                req.Method = "HEAD";

                System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse();
                resp.Close(); return true;

Is any other good way to check internet connectivity status.
All your Suggestions are appreciable!!!

If you are using .net 4, use Ping and an internet address like : (Google's DNS)[^]
Share this answer
VJ Reddy 22-May-12 3:50am    
Good reference. 5!
Mehdi Gholam 22-May-12 4:40am    
Thanks VJ!
When using the second approach or the solution provided by Mehdi Gholam, you may want to make sure that you are doing this from a thread that is not your main thread since this would cause the UI to hang.
Share this answer
Mehdi Gholam 22-May-12 4:40am    
A good point! 5'ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900