How to query internet connection status in C# ?
what is best and Quick 开发者_开发技巧way to find user connected to internet or not ?
try this:
1: /// <summary>
2: /// Performs actions on the network
3: /// </summary>
4: public sealed class NetworkHandler
5: {
6: /// <summary>
7: /// Private constructor to prevent compiler from generating one
8: /// since this class only holds static methods and properties
9: /// </summary>
10: NetworkHandler() { }
11:
12: /// <summary>
13: /// SafeNativeMethods Class that holds save native methods
14: /// while suppressing unmanaged code security
15: /// </summary>
16: [SuppressUnmanagedCodeSecurityAttribute]
17: internal static class SafeNativeMethods
18: {
19: // Extern Library
20: // UnManaged code - be careful.
21: [DllImport("wininet.dll", CharSet = CharSet.Auto)]
22: [return: MarshalAs(UnmanagedType.Bool)]
23: private extern static bool
24: InternetGetConnectedState(out int Description, int ReservedValue);
25:
26: /// <summary>
27: /// Determines if there is an active connection on this computer
28: /// </summary>
29: /// <returns></returns>
30: public static bool HasActiveConnection()
31: {
32: int desc;
33: return InternetGetConnectedState(out desc, 0);
34: }
35: }
36: }
Source: http://blog.dotnetclr.com/archive/2007/09/24/Check-for-internet-connection---Method-2.aspx
"Connected to the internet" is a pretty fuzzy term. You can either try to access a specific resource on the internet, or go for a more general approach and see if you can access the default gateway. Both have their shortcomings. In the first case the specific site can be down, but other internet access could be okay. On the other hand if you cannot see the default gateway, you can't access the internet, however if you can access the default gateway you may still not be able to access the needed resources on the net.
Keep in mind that "access" means different things here. You could ping the resource, but there are many resources that do not answer to ping (or ping could be blocked along the way). I.e. if the resource doesn't answer ping request you cannot necessarily conclude that it is unavailable.
Your best bet is probably to try to do whatever you need to do on the internet and then handle exceptions as they occur.
Ping google.com?
Edit: But how to do that?
A fast and simple solution would be to do a web get to www.google.com and see if you can download anything:
Using sockets/TcpClient:
try
{
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("www.google.com",80);
client.Close();
}
catch (SocketException)
{
// Offline
}
Or you could try a dns lookup:
try
{
System.Net.IPHostEntry ipHostEntry= System.Net.Dns.GetHostEntry("www.google.com");
}
catch(SocketException)
{
// Offline
}
Or you would try a web request:
try
{
System.New.WebRequest.Create("http://www.google.com").GetResponse();
}
catch(WebException)
{
// Offline
}
Ping any site Demo: http://www.dreamincode.net/code/snippet1568.htm and you find find some tips here check whether Internet connection is available with C#
Try to use Ping class and check exact IP addresses
You would be far better off to try whatever operation you need to do, and then deal with failure in an appropriate way at the time. You'd need to do that anyway. Think about it:
- You check for a connection
- The connection drops out a microsecond later
- You try to fetch something (or send something), thinking you're still connected
You will fail at step 3 despite your check, and you'll need to deal with that anyway. (Worse still, step 3 starts fine but the connection drops halfway through.) So why bother with step 1?
AFAIK You cannot test an internet connection without trying to connect to a website. So the best bet would be to try Google.com as Rune said. Which loads faster than most fot he websites.
DNS Lookup:
System.Net.Dns.GetHostAddresses("google.com")
Faster than ping. This should reliably determine whether the user is connected to the internet. In some weird scenarios you might get a false negative or false positive.
If google.com is in the hosts file for example (chance 0.000000000001%)
or if the user is connected but his DNS servers do not work / are not configured.
精彩评论