2using System.Threading.Tasks;
15 #region String-valued settings
24 public static string Get(
string EnvironmentVariable,
string Key,
string DefaultValue)
26 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
36 public static Task<string>
GetAsync(
string EnvironmentVariable,
string Key,
string DefaultValue)
38 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
42 return Task.FromResult(s);
47 #region Int64-valued settings
56 public static long Get(
string EnvironmentVariable,
string Key,
long DefaultValue)
58 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
68 public static Task<long>
GetAsync(
string EnvironmentVariable,
string Key,
long DefaultValue)
70 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
71 if (s is
null || !
long.TryParse(s, out
long Result))
74 return Task.FromResult(Result);
79 #region Boolean-valued settings
88 public static bool Get(
string EnvironmentVariable,
string Key,
bool DefaultValue)
90 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
100 public static Task<bool>
GetAsync(
string EnvironmentVariable,
string Key,
bool DefaultValue)
102 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
106 return Task.FromResult(Result);
111 #region DateTime-valued settings
120 public static DateTime
Get(
string EnvironmentVariable,
string Key, DateTime DefaultValue)
122 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
132 public static Task<DateTime>
GetAsync(
string EnvironmentVariable,
string Key, DateTime DefaultValue)
134 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
138 return Task.FromResult(Result.LocalDateTime);
140 return Task.FromResult(Result2);
141 else if (DateTime.TryParse(s, out Result2))
142 return Task.FromResult(Result2);
149 #region TimeSpan-valued settings
158 public static TimeSpan
Get(
string EnvironmentVariable,
string Key, TimeSpan DefaultValue)
160 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
170 public static Task<TimeSpan>
GetAsync(
string EnvironmentVariable,
string Key, TimeSpan DefaultValue)
172 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
173 if (s is
null || !TimeSpan.TryParse(s, out TimeSpan Result))
176 return Task.FromResult(Result);
181 #region Double-valued settings
190 public static double Get(
string EnvironmentVariable,
string Key,
double DefaultValue)
192 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
202 public static Task<double>
GetAsync(
string EnvironmentVariable,
string Key,
double DefaultValue)
204 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
208 return Task.FromResult(Result);
213 #region Enum-valued settings
222 public static Enum
Get(
string EnvironmentVariable,
string Key, Enum DefaultValue)
224 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
234 public static Task<Enum>
GetAsync(
string EnvironmentVariable,
string Key, Enum DefaultValue)
236 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
240 Type T = DefaultValue.GetType();
241 string[] Names = Enum.GetNames(T);
242 if (Array.IndexOf(Names, s) < 0)
245 return Task.FromResult((Enum)Enum.Parse(T, s));
Helps with parsing of commong data types.
static bool TryParseRfc822(string s, out DateTimeOffset Value)
Parses a date and time value encoded according to RFC 822, §5.
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Helps with common XML-related tasks.
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Static class managing persistent environment settings. Environment settings default to runtime settin...
static DateTime Get(string EnvironmentVariable, string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static Enum Get(string EnvironmentVariable, string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static Task< bool > GetAsync(string EnvironmentVariable, string Key, bool DefaultValue)
Gets a bool-valued setting.
static Task< Enum > GetAsync(string EnvironmentVariable, string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static Task< TimeSpan > GetAsync(string EnvironmentVariable, string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static long Get(string EnvironmentVariable, string Key, long DefaultValue)
Gets a long-valued setting.
static Task< double > GetAsync(string EnvironmentVariable, string Key, double DefaultValue)
Gets a double-valued setting.
static Task< string > GetAsync(string EnvironmentVariable, string Key, string DefaultValue)
Gets a string-valued setting.
static bool Get(string EnvironmentVariable, string Key, bool DefaultValue)
Gets a bool-valued setting.
static double Get(string EnvironmentVariable, string Key, double DefaultValue)
Gets a double-valued setting.
static string Get(string EnvironmentVariable, string Key, string DefaultValue)
Gets a string-valued setting.
static Task< DateTime > GetAsync(string EnvironmentVariable, string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static TimeSpan Get(string EnvironmentVariable, string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static Task< long > GetAsync(string EnvironmentVariable, string Key, long DefaultValue)
Gets a long-valued setting.
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.