Guest User

Windows 8 check for Internet connection

a guest
Apr 23rd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. public static bool IsInternetSlow()
  2. {
  3.     return NetworkInterface.GetIsNetworkAvailable();
  4. }
  5.  
  6. public static bool IsInternet()
  7. {
  8.     ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
  9.     bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
  10.     return internet;
  11. }
  12.  
  13. protected override void Test()
  14. {
  15.     Stopwatch w = new Stopwatch();
  16.     w.Start();
  17.     for (int i = 0; i < 100; i++)
  18.     {
  19.         bool isInternet = IsInternetSlow();
  20.     }
  21.  
  22.     w.Stop();
  23.     Debug.WriteLine("Slow method: {0}", w.Elapsed);
  24.  
  25.     w.Reset();
  26.     w.Start();
  27.     for (int i = 0; i < 100; i++)
  28.     {
  29.         bool isInternet = IsInternet();
  30.     }
  31.  
  32.     w.Stop();
  33.     Debug.WriteLine("Fast method: {0}", w.Elapsed);
  34. }
  35.  
  36. /////////////////////////////
  37. Slow method: 00:00:17.1382156
  38. Fast method: 00:00:01.5759819
Advertisement
Add Comment
Please, Sign In to add comment