I am making an windows 8 app which involves internet connectivity. Before starting with the functions of app, I want to check whether the device is connected to the internet or not. Could anyone tell the try catch block statements associated with the process. Thank You in advance!
Asked
Active
Viewed 1,029 times
0
-
2Have you made any attempt at this? Even something like just making a request to google.com and checking for a valid response? – David Dec 04 '13 at 18:28
-
http://stackoverflow.com/questions/13625304/check-internet-connection-availability-in-windows-8 – Jon Dec 04 '13 at 18:43
-
possible duplicate of [What is the best way to check for Internet connectivity using .Net?](http://stackoverflow.com/questions/2031824/what-is-the-best-way-to-check-for-internet-connectivity-using-net) – Louis Ricci Dec 04 '13 at 18:45
1 Answers
0
Why not use something like:
public static bool CheckForInternetConnection()
{
try
{
using (var mobileClient = new WebClient())
using (var webConnection = mobileClient.OpenRead("http://www.somewebsite.com"))
{
return true;
}
}
catch
{
return false;
}
}
The WebClient
class lives in the System.Net.WebClient
namespace, so you will need to add a reference to it. The documentation for WebClient
can be found here.

Brian
- 5,069
- 7
- 37
- 47
-
-
http://stackoverflow.com/a/2031831/884862 the original answer, top search result for --c# check internet-- – Louis Ricci Dec 04 '13 at 18:44
-
Your point being? I used that answer (which I also Google'd) in a solution I was working on and copied out the code above from said solution. – Brian Dec 04 '13 at 18:50
-
a `WebClient` just for testing internet connection? And pinging an external site? this is plain **bad**. please use something like this instead : http://stackoverflow.com/a/29701786/953684 – Sharky Nov 16 '15 at 13:05