如何检测工作在C#中的互联网连接? [英] How to detect working internet connection in C#?
问题描述
我有一个C#code通过FTP协议,基本上上传文件(使用的FtpWebRequest
)。我想,但是,首先确定是否有要上传的文件(因为,如果没有有试图,该软件应该只是睡了一段时间,并再次检查无点)之前的因特网连接
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?
推荐答案
只需使用普通的功能
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
如果连接了返回的虚假事实。
that return true of false if the connection is up.
<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getisnetworkavailable.aspx\">From MSDN:
的网络连接被认为是可用的,如果任何网络接口被标记为向上,而不是一个回环或隧道接口。的
请是不是所有的,可以被连接到本地网络和路由器未连接到互联网的实例。要真正知道你是否连接到互联网,尝试平类
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.
这篇关于如何检测工作在C#中的互联网连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!