How do you determine if an Internet connection is available for your WinForms App?

前端 未结 8 802
生来不讨喜
生来不讨喜 2020-12-24 15:33

What is the best way to determine whether there is an available Internet connection for a WinForms app. (Programatically of course) I want to disable/hide certain functions

相关标签:
8条回答
  • 2020-12-24 15:58

    Ping google.com (or a list of well-known hosts) or try actually performing one of the functions (in a structural sense) for which your application requires Internet connectivity. There is no way, on any operating system, to truly determine whether or not Internet connectivity is functional without actually trying to communicate, as opposed to the operating system's view on what constitutes "available".

    0 讨论(0)
  • 2020-12-24 16:05

    I found this code elsewhere, but really want to know if there is a better way.

    HttpWebRequest req;
    HttpWebResponse resp;
    try
    {
        req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
        resp = (HttpWebResponse)req.GetResponse();
    
        if(resp.StatusCode.ToString().Equals("OK"))
        {
            Console.WriteLine("its connected.");
        }
        else
        {
            Console.WriteLine("its not connected.");
        }
    }
    catch(Exception exc)
    {
        Console.WriteLine("its not connected.");
    }
    

    I like the idea of being able to monitor if the connection is lost via the NetworkChange event thrown by NetworkInterface. My app is for use by inexperienced users, on notebooks, where Internet connectivity is erratic (often in the Australian Outback).

    0 讨论(0)
  • 2020-12-24 16:08

    sbeskur's response has a bug in the translation of InternetGetConnectedState. The parameters are both DWORD's (first one is an LPDWORD). Both of these translate to int's in C# (technically, uint but int will work for most scenarios).

    Correct translation below.

    
    [DllImport("wininet.dll", SetLastError=true)] 
    public static extern bool InternetGetConnectedState(out int flags,int reserved);
    
    0 讨论(0)
  • 2020-12-24 16:12

    I'm not a c# developer but in C++ you can use the Win32 API (specifically from Wininet.dll) like this:

    bool IsInternetConnected( void )
    {
        DWORD dwConnectionFlags = 0;
    
        if( !InternetGetConnectedState( &dwConnectionFlags, 0 ) )
            return false;
    
        if( InternetAttemptConnect( 0 ) != ERROR_SUCCESS )
            return false;
    
        return true;
    }
    

    I assume this is trivially turned into c#

    0 讨论(0)
  • 2020-12-24 16:13

    http://www.csharphelp.com/archives3/archive499.html

    Also, scroll past the experts-exchange links at: http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=13045, and you'll see some suggestions.

    Also, if you are game for the My namespace from VB.Net (which you can link to, btw), My.Computer.Network.IsAvailable is the simplest solution.

    0 讨论(0)
  • 2020-12-24 16:14

    InternetGetConnected state is step one in establishing that you are connected to a network. In order to determine if you have an internet connection one technique is to use the IPHelper api to send an ARP (address resolution protocol) request for some server on the internet.

    0 讨论(0)
提交回复
热议问题