Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

ftpwebrequest - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...