| | 1 | | using System.Security; |
| | 2 | |
|
| | 3 | | namespace Songhay.Extensions; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Extensions of <see cref="Environment"/>. |
| | 7 | | /// </summary> |
| | 8 | | public static class EnvironmentExtensions |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// The conventional <see cref="ICollection{T}"/> |
| | 12 | | /// of <see cref="SystemVariable"/>. |
| | 13 | | /// </summary> |
| 0 | 14 | | public static readonly Lazy<ICollection<SystemVariable>> EnvironmentVariables = |
| 0 | 15 | | new(ListEnvironmentVariables, LazyThreadSafetyMode.PublicationOnly); |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Gets the value from the conventional <see cref="ICollection{T}"/> |
| | 19 | | /// of <see cref="SystemVariable"/> based on the specified variable name. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="conventionalName">The <see cref="SystemVariable.VariableName"/>.</param> |
| | 22 | | /// <returns>Returns <see cref="SystemVariable.VariableValue"/>.</returns> |
| | 23 | | public static string? GetConventionalValue(string? conventionalName) |
| 0 | 24 | | { |
| 0 | 25 | | return EnvironmentVariables.Value |
| 0 | 26 | | .Where(e => e.VariableName.EqualsInvariant(conventionalName)) |
| 0 | 27 | | .Select(e => e.VariableValue).First(); |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | static ICollection<SystemVariable> ListEnvironmentVariables() |
| 0 | 31 | | { |
| 0 | 32 | | var list = new List<SystemVariable> |
| 0 | 33 | | { |
| 0 | 34 | | new() |
| 0 | 35 | | { |
| 0 | 36 | | VariableName = nameof(Environment.MachineName), |
| 0 | 37 | | VariableDescription = "Network Identification", |
| 0 | 38 | | VariableValue = Environment.MachineName |
| 0 | 39 | | }, |
| 0 | 40 | | new() |
| 0 | 41 | | { |
| 0 | 42 | | VariableName = $"{nameof(Environment.OSVersion)}.{nameof(Environment.OSVersion.Platform)}", |
| 0 | 43 | | VariableDescription = "Operating System Platform", |
| 0 | 44 | | VariableValue = Environment.OSVersion.Platform.ToString() |
| 0 | 45 | | }, |
| 0 | 46 | | new() |
| 0 | 47 | | { |
| 0 | 48 | | VariableName = $"{nameof(Environment.OSVersion)}.{nameof(Environment.OSVersion.ServicePack)}", |
| 0 | 49 | | VariableDescription = "Operating System Service Pack", |
| 0 | 50 | | VariableValue = Environment.OSVersion.ServicePack |
| 0 | 51 | | }, |
| 0 | 52 | | new() |
| 0 | 53 | | { |
| 0 | 54 | | VariableName = $"{nameof(Environment.OSVersion)}.{nameof(Environment.OSVersion.VersionString)}", |
| 0 | 55 | | VariableDescription = "Operating System Version Summary", |
| 0 | 56 | | VariableValue = Environment.OSVersion.VersionString |
| 0 | 57 | | }, |
| 0 | 58 | | new() |
| 0 | 59 | | { |
| 0 | 60 | | VariableName = nameof(Environment.UserDomainName), |
| 0 | 61 | | VariableDescription = "User Domain Name", |
| 0 | 62 | | VariableValue = Environment.UserDomainName |
| 0 | 63 | | }, |
| 0 | 64 | | new() |
| 0 | 65 | | { |
| 0 | 66 | | VariableName = nameof(Environment.UserName), |
| 0 | 67 | | VariableDescription = "User Name", |
| 0 | 68 | | VariableValue = Environment.UserName |
| 0 | 69 | | }, |
| 0 | 70 | | new() |
| 0 | 71 | | { |
| 0 | 72 | | VariableName = nameof(Environment.Version.Major), |
| 0 | 73 | | VariableDescription = "CLR Major Version", |
| 0 | 74 | | VariableValue = Environment.Version.Major.ToString(CultureInfo.InvariantCulture) |
| 0 | 75 | | }, |
| 0 | 76 | | new() |
| 0 | 77 | | { |
| 0 | 78 | | VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.MajorRevision)}", |
| 0 | 79 | | VariableDescription = "CLR Major Revision", |
| 0 | 80 | | VariableValue = Environment.Version.MajorRevision.ToString(CultureInfo.InvariantCulture) |
| 0 | 81 | | }, |
| 0 | 82 | | new() |
| 0 | 83 | | { |
| 0 | 84 | | VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.Minor)}", |
| 0 | 85 | | VariableDescription = "CLR Minor Version", |
| 0 | 86 | | VariableValue = Environment.Version.Minor.ToString(CultureInfo.InvariantCulture) |
| 0 | 87 | | }, |
| 0 | 88 | | new() |
| 0 | 89 | | { |
| 0 | 90 | | VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.MinorRevision)}", |
| 0 | 91 | | VariableDescription = "CLR Minor Revision", |
| 0 | 92 | | VariableValue = Environment.Version.MinorRevision.ToString(CultureInfo.InvariantCulture) |
| 0 | 93 | | }, |
| 0 | 94 | | new() |
| 0 | 95 | | { |
| 0 | 96 | | VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.Revision)}", |
| 0 | 97 | | VariableDescription = "CLR Revision", |
| 0 | 98 | | VariableValue = Environment.Version.Revision.ToString(CultureInfo.InvariantCulture) |
| 0 | 99 | | } |
| 0 | 100 | | }; |
| | 101 | |
|
| | 102 | | #region Insert data into list: |
| | 103 | |
|
| | 104 | | try |
| 0 | 105 | | { |
| 0 | 106 | | foreach (DictionaryEntry environment in Environment.GetEnvironmentVariables( |
| 0 | 107 | | EnvironmentVariableTarget.Machine)) |
| 0 | 108 | | { |
| 0 | 109 | | if (string.IsNullOrWhiteSpace(environment.Value?.ToString())) continue; |
| | 110 | |
|
| 0 | 111 | | list.Add(new SystemVariable |
| 0 | 112 | | { |
| 0 | 113 | | VariableName = $"{nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Machine)} [ke |
| 0 | 114 | | VariableDescription = "Machine Environment Variables", |
| 0 | 115 | | VariableValue = environment.Value?.ToString() |
| 0 | 116 | | }); |
| 0 | 117 | | } |
| 0 | 118 | | } |
| 0 | 119 | | catch (SecurityException ex) |
| 0 | 120 | | { |
| 0 | 121 | | list.Add(new SystemVariable |
| 0 | 122 | | { |
| 0 | 123 | | VariableName = $"EXCEPTION! {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Machin |
| 0 | 124 | | VariableDescription = $"EXCEPTION for {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTar |
| 0 | 125 | | VariableValue = $"Message: {ex.Message}\nGranted Set: {ex.GrantedSet}\nPermission State: {ex.PermissionS |
| 0 | 126 | | }); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | try |
| 0 | 130 | | { |
| 0 | 131 | | foreach (DictionaryEntry environment in Environment.GetEnvironmentVariables( |
| 0 | 132 | | EnvironmentVariableTarget.Process)) |
| 0 | 133 | | { |
| 0 | 134 | | if (string.IsNullOrWhiteSpace(environment.Value?.ToString())) continue; |
| | 135 | |
|
| 0 | 136 | | list.Add(new SystemVariable |
| 0 | 137 | | { |
| 0 | 138 | | VariableName = $"{nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Process)} [ke |
| 0 | 139 | | VariableDescription = "Process Environment Variables", |
| 0 | 140 | | VariableValue = environment.Value?.ToString() |
| 0 | 141 | | }); |
| 0 | 142 | | } |
| 0 | 143 | | } |
| 0 | 144 | | catch (SecurityException ex) |
| 0 | 145 | | { |
| 0 | 146 | | list.Add(new SystemVariable |
| 0 | 147 | | { |
| 0 | 148 | | VariableName = $"EXCEPTION! {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Proces |
| 0 | 149 | | VariableDescription = $"EXCEPTION for {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTar |
| 0 | 150 | | VariableValue = $"Message: {ex.Message}\nGranted Set: {ex.GrantedSet}\nPermission State: {ex.PermissionS |
| 0 | 151 | | }); |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | try |
| 0 | 155 | | { |
| 0 | 156 | | foreach (DictionaryEntry environment in Environment.GetEnvironmentVariables( |
| 0 | 157 | | EnvironmentVariableTarget.User)) |
| 0 | 158 | | { |
| 0 | 159 | | if (string.IsNullOrWhiteSpace(environment.Value?.ToString())) continue; |
| | 160 | |
|
| 0 | 161 | | list.Add(new SystemVariable |
| 0 | 162 | | { |
| 0 | 163 | | VariableName = $"{nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.User)} [key: |
| 0 | 164 | | VariableDescription = "User Environment Variables", |
| 0 | 165 | | VariableValue = environment.Value?.ToString() |
| 0 | 166 | | }); |
| 0 | 167 | | } |
| 0 | 168 | | } |
| 0 | 169 | | catch (SecurityException ex) |
| 0 | 170 | | { |
| 0 | 171 | | list.Add(new SystemVariable |
| 0 | 172 | | { |
| 0 | 173 | | VariableName = $"EXCEPTION! {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.User)} |
| 0 | 174 | | VariableDescription = $"EXCEPTION for {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTar |
| 0 | 175 | | VariableValue = $"Message: {ex.Message}\nGranted Set: {ex.GrantedSet}\nPermission State: {ex.PermissionS |
| 0 | 176 | | }); |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | #endregion |
| | 180 | |
|
| 0 | 181 | | return list; |
| 0 | 182 | | } |
| | 183 | | } |