comparison ServerMonitor/Helpers.cs @ 4:3142e52cbe69

Lots more progress
author Brad Greco <brad@bgreco.net>
date Sun, 10 Feb 2019 20:51:26 -0500
parents 453ecc1ed9ea
children b6fe203af9d5
comparison
equal deleted inserted replaced
3:96f0b028176d 4:3142e52cbe69
2 using System; 2 using System;
3 using System.Collections.Generic; 3 using System.Collections.Generic;
4 using System.ComponentModel; 4 using System.ComponentModel;
5 using System.Drawing; 5 using System.Drawing;
6 using System.Linq; 6 using System.Linq;
7 using System.Net.NetworkInformation;
7 using System.Text; 8 using System.Text;
8 using System.Windows.Forms; 9 using System.Windows.Forms;
9 10
10 namespace ServerMonitorApp 11 namespace ServerMonitorApp
11 { 12 {
48 case CheckStatus.Error: return Resources.error; 49 case CheckStatus.Error: return Resources.error;
49 case CheckStatus.Warning: return Resources.warning; 50 case CheckStatus.Warning: return Resources.warning;
50 case CheckStatus.Information: return Resources.info; 51 case CheckStatus.Information: return Resources.info;
51 case CheckStatus.Success: return Resources.pass; 52 case CheckStatus.Success: return Resources.pass;
52 case CheckStatus.Running: return Resources.run; 53 case CheckStatus.Running: return Resources.run;
54 case CheckStatus.Disabled: return Resources.disable;
53 default: return null; 55 default: return null;
54 } 56 }
55 } 57 }
56 58
57 public static bool In(this Enum value, params Enum[] values) { 59 public static bool In(this Enum value, params Enum[] values) {
58 return values.Contains(value); 60 return values.Contains(value);
59 } 61 }
62
63 // https://stackoverflow.com/a/8345173
64 /// <summary>
65 /// Indicates whether any network connection is available.
66 /// Filter connections below a specified speed, as well as virtual network cards.
67 /// </summary>
68 /// <returns>
69 /// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
70 /// </returns>
71 public static bool IsNetworkAvailable()
72 {
73 if (!NetworkInterface.GetIsNetworkAvailable())
74 return false;
75
76 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
77 {
78 // discard because of standard reasons
79 if ((ni.OperationalStatus != OperationalStatus.Up) ||
80 (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
81 (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
82 continue;
83
84 // discard virtual cards (virtual box, virtual pc, etc.)
85 if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
86 (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
87 continue;
88
89 // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
90 if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
91 continue;
92
93 return true;
94 }
95 return false;
96 }
60 } 97 }
61 } 98 }