annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
1 using ServerMonitorApp.Properties;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2 using System;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.Collections.Generic;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.ComponentModel;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Drawing;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Linq;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
7 using System.Net.NetworkInformation;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 using System.Windows.Forms;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 static class Helpers
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 public static void FormatImageButton(Button button)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 public static string GetAllMessages(this Exception ex)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 public static string GetDisplayName(Type type)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 public static bool IsNullOrEmpty(this string aString)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32 return aString == null || aString.Trim() == string.Empty;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
35 public static string ConvertNewlines(this string aString)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
36 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
37 return aString.Replace("\r\n", "\n").Replace('\r', '\n');
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
38 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
39
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40 public static T GetAttribute<T>(this Type type) where T : Attribute
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 public static Image GetIcon(this CheckStatus checkStatus)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47 switch (checkStatus)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 case CheckStatus.Error: return Resources.error;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 case CheckStatus.Warning: return Resources.warning;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 case CheckStatus.Information: return Resources.info;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 case CheckStatus.Success: return Resources.pass;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 case CheckStatus.Running: return Resources.run;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
54 case CheckStatus.Disabled: return Resources.disable;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55 default: return null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 public static bool In(this Enum value, params Enum[] values) {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 return values.Contains(value);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
62
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
63 // https://stackoverflow.com/a/8345173
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
64 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
65 /// Indicates whether any network connection is available.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
66 /// Filter connections below a specified speed, as well as virtual network cards.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
67 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
68 /// <returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
69 /// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
70 /// </returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
71 public static bool IsNetworkAvailable()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
72 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
73 if (!NetworkInterface.GetIsNetworkAvailable())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
74 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
75
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
76 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
77 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
78 // discard because of standard reasons
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
79 if ((ni.OperationalStatus != OperationalStatus.Up) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
80 (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
81 (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
82 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
83
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
84 // discard virtual cards (virtual box, virtual pc, etc.)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
85 if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
86 (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
87 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
88
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
89 // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
90 if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
91 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
92
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
93 return true;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
94 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
95 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
96 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
97 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 }