Mercurial > servermonitor
annotate ServerMonitor/Helpers.cs @ 40:c4fc74593a78 default tip
Mono fix
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sat, 13 Jun 2020 13:28:20 -0400 |
parents | 7645122aa7a9 |
children |
rev | line source |
---|---|
17 | 1 using ServerMonitorApp.Properties; |
0
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.ComponentModel; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
4 using System.Drawing; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
5 using System.Linq; |
4 | 6 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
|
7 using System.Windows.Forms; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
8 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
9 namespace ServerMonitorApp |
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 static class Helpers |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
12 { |
17 | 13 /// <summary>Resizes the image on a button to fit inside the button's dimensions.</summary> |
14 /// <param name="button">A button containing an image.</param> | |
0
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 |
17 | 20 /// <summary>Recursively collects the messages for an exception and its inner exceptions.</summary> |
21 /// <param name="ex">The parent exception.</param> | |
22 /// <returns>A string containing the messages for the exception and its inner exceptions.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
23 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
|
24 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
25 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
|
26 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
27 |
17 | 28 /// <summary>Gets the value of the DisplayNameAttribute for a type.</summary> |
29 /// <param name="type">The type to get the display name of.</param> | |
30 /// <returns>The type's display name, or the type name if no display name is defined.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
31 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
|
32 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
33 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
|
34 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
35 |
17 | 36 /// <summary>Checks whether a string is null, an empty string, or only contains white space.</summary> |
37 /// <param name="aString">The string to test.</param> | |
38 /// <returns>True if the string is null, an empty string, or only contains white space. False otherwise.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
39 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
|
40 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
41 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
|
42 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
43 |
17 | 44 /// <summary>Converts all newlines in a string to unix format.</summary> |
45 /// <param name="aString">The string to convert.</param> | |
46 /// <returns>The string with all newlines converted to unix format.</returns> | |
2 | 47 public static string ConvertNewlines(this string aString) |
48 { | |
49 return aString.Replace("\r\n", "\n").Replace('\r', '\n'); | |
50 } | |
51 | |
17 | 52 /// <summary>Gets an attribute on a class.</summary> |
53 /// <typeparam name="T">The type of the attribute to return.</typeparam> | |
54 /// <param name="type">The type of the class the attribute is on.</param> | |
55 /// <returns>The attribute, or null if the attribute does not exist on the class.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
56 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
|
57 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
58 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
|
59 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
60 |
17 | 61 /// <summary>Gets an image associated with a check status for use in the UI.</summary> |
62 /// <param name="checkStatus">The check status.</param> | |
63 /// <returns>The image associated with the check status.</returns> | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
64 public static Image GetImage(this CheckStatus checkStatus) |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
65 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
66 switch (checkStatus) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
67 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 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
|
72 case CheckStatus.Running: return Resources.run; |
4 | 73 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
|
74 default: return null; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
75 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
76 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
77 |
17 | 78 /// <summary>Gets a program icon associated with a check status.</summary> |
79 /// <param name="checkStatus">The check status.</param> | |
80 /// <returns>The program icon associated with the check status.</returns> | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
81 public static Icon GetIcon(this CheckStatus checkStatus) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
82 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
83 switch (checkStatus) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
84 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
85 case CheckStatus.Error: return Resources.icon_error; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
86 case CheckStatus.Warning: return Resources.icon_warning; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
87 case CheckStatus.Information: return Resources.icon_info; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
88 default: return Resources.icon; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
89 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
90 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
91 |
17 | 92 /// <summary>Returns whether an enum is in a list of values.</summary> |
93 /// <param name="value">The value to check.</param> | |
94 /// <param name="values">The list of possible values.</param> | |
95 /// <returns>True if the value was in the list, false otherwise.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
96 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
|
97 return values.Contains(value); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
98 } |
4 | 99 |
100 // https://stackoverflow.com/a/8345173 | |
101 /// <summary> | |
102 /// Indicates whether any network connection is available. | |
103 /// Filter connections below a specified speed, as well as virtual network cards. | |
104 /// </summary> | |
105 /// <returns> | |
106 /// <c>true</c> if a network connection is available; otherwise, <c>false</c>. | |
107 /// </returns> | |
108 public static bool IsNetworkAvailable() | |
109 { | |
110 if (!NetworkInterface.GetIsNetworkAvailable()) | |
111 return false; | |
112 | |
113 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) | |
114 { | |
115 // discard because of standard reasons | |
116 if ((ni.OperationalStatus != OperationalStatus.Up) || | |
117 (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) || | |
118 (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel)) | |
119 continue; | |
120 | |
121 // discard virtual cards (virtual box, virtual pc, etc.) | |
122 if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) || | |
123 (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0)) | |
124 continue; | |
125 | |
126 // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card. | |
127 if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase)) | |
128 continue; | |
129 | |
130 return true; | |
131 } | |
132 return false; | |
133 } | |
39 | 134 |
135 /// <summary>Returns whether the program is running on Mono.</summary> | |
136 /// <returns>True if the program is running on Mono.</returns> | |
137 public static bool IsMono() | |
138 { | |
139 return Type.GetType("Mono.Runtime") != null; | |
140 } | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
141 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
142 } |