Checking for internet connection in silverlight

When I call my Doggy, I am pretty sure of two things, either he is at home or have gone out with his girlfriend next door (yes, he has one). When he don't respond I am sure that he is not at home and might be enjoying his evening somewhere. But for the situations such as doing some very personal stuffs, I need to get sure that he is at home or not. For that, either I'll call him or will go and search for him. The later, in fact, is the better option to get very much sure that he is not at home since I'd already told you that he is jealous of me.

You might say why the hell we at any situation got to know that whether the internet connection is there or not? We use WCF RIA or WCF services whatever to communicate with the database, if there is no internet connection the queries will automatically stop responding and giving us connectivity errors. Fine, but what if you need to do a very internet related task and you ain't performing any service call, such as displaying a webpage? You need to be pretty sure of internet connectivity before you do the stuff.

Here is just a line of code that help you with that:

   1: if (NetworkInterface.GetIsNetworkAvailable())
   2: {
   3:     // there is internet :)
   4: }
   5: else
   6: {
   7:     // no internet :(
   8: }

Networkinterface class, comes under System.Net.NetworkInformation namespace. It's an abstract class by the way. Any ways, just a single line of code and so cool that can save you from Page not found like errors…

Happy Reading!!!

Ebook Download
View all
Learn
View all