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 fo开发者_运维百科r 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?
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.
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);
}
}
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.
Can't you just use the Ping Class of the System.Net.NetworkInformation Namespace to ping the FTP server before trying to upload the file?
Think about the situation where your check comes back and says "the connection is there", and before you can start your FTP, the connection drops.
Or where the connection drops part way through your FTP request.
Given that you have to code for these situations anyway, just skip the check
Edit in response to Jason's comments
You can also have the opposite condition occur - that when you check for a connection, none exists, but a moment later, their connection comes up. So now what do you do - do you start nagging the user about the absence of a connection, even though it's now available?
At the end of the day, you're dealing with a large number of resources (your net connection, any intermediate routers, the host, its FTP service). All of these are subject to change outside of your control (as Seth's comment indicated), and no amount of pre-testing will answer the question "will I be able to complete this upload"?
As the OP indicated that he's thinking of a "back off and try again later" approach, then I think it's appropriate to do all of that in the background and not annoy the user at all - unless you've been trying for an "unreasonable" amount of time without success.
if ping is difficult for you, just use webclient.
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
catch
{
return false;
}
}
or any other site. EDIT : you can use http://www.msftncsi.com/ this site. This is site which is run only for internet connectivity. See detailed registry explanation of how windows 7 finds internet connectivity http://blog.superuser.com/2011/05/16/windows-7-network-awareness/
just wrote async functions to do that:
private void myPingCompletedCallback(object sender, PingCompletedEventArgs e)
{
if (e.Cancelled)
return;
if (e.Error != null)
return;
if (e.Reply.Status == IPStatus.Success)
{
//ok connected to internet, do something...
}
}
private void checkInternet()
{
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(myPingCompletedCallback);
try
{
myPing.SendAsync("google.com", 3000 /*3 secs timeout*/, new byte[32], new PingOptions(64, true));
}
catch
{
}
}
精彩评论