Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect working internet connection in C#?

I have a C# code that basically uploads a file via FTP protocol (using FtpWebRequest). I'd like, however, to first determine whether there is a working internet connection before trying to upload the file (since, if there isn't there is no point in trying, the software should just sleep for a time and check again).

Is there an easy way to do it or should I just try to upload the file and in case it failed just try again, assuming the network connection was down?

like image 723
Detariael Avatar asked Mar 26 '10 06:03

Detariael


3 Answers

Just use the plain function

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

that return true of false if the connection is up.

From MSDN: A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.

Keep in mind connectivity is not all, you can be connected to a local network and the router is not connected to the Internet for instance. To really know if you are connected to the internet try the Ping class.

like image 99
Zyo Avatar answered Sep 29 '22 02:09

Zyo


There is a "network availability changed" event which fires when the "up" state of a network connection changes on an interface that is not a tunnel or loopback.

You could read the state of all network adapters on the system at startup, store the current value of "network is available" then listen for this event and change your network state variable when this event fires. This also looks like it will handle dial-up and ISDN connections too.

Granted there are other factors to take into account, such as the NIC is connected to a router (and working) but the Internet connection on the router is down, or the remote host is not responding, but this will at least prevent you trying to make a connection that isn't going to work if there's no network connection to begin with (e.g. VPN or ISDN link is down.)

This is a C# console application - start it running, then disable or unplug your network connection :-)

class Program
{
    static bool networkIsAvailable = false;

    static void Main(string[] args)
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface nic in nics)
        {
            if (
                (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel) &&
                nic.OperationalStatus == OperationalStatus.Up)
            {
                networkIsAvailable = true;
            }
        }

        Console.Write("Network availability: ");
        Console.WriteLine(networkIsAvailable);

        NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

        Console.ReadLine();
    }

    static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
    {
        networkIsAvailable = e.IsAvailable;

        Console.Write("Network availability: ");
        Console.WriteLine(networkIsAvailable);
    }
}
like image 42
Andy Shellam Avatar answered Sep 29 '22 02:09

Andy Shellam


I think the best approximation you can use is to check the OperationalStatus value on the NetworkInterface type.

using System.Net.NetworkInformation;

public bool IsNetworkLikelyAvailable() {
  return NetworkInterface
    .GetAllNetworkInterfaces()
    .Any(x => x.OperationalStatus == OperationalStatus.Up);
}

Remember though this is an approximation. The moment this method returns the computer could lose or gain it's internet connection. IMO I would just go straight for the upload and handle the error since you can't prove it won't happen.

like image 45
JaredPar Avatar answered Sep 29 '22 02:09

JaredPar