Click here to Skip to main content
15,886,059 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 requirement.it returns true even only network is connected and internet disabled.

C#
[DllImport("wininet.dll")]
  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,
C#
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.google.com");
                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!!!
Posted

If you are using .net 4, use Ping and an internet address like : 8.8.8.8 (Google's DNS)
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx[^]
 
Share this answer
 
Comments
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
 
Comments
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