/////////////////////////////////////////////////////////////////////// public static void UnsetValue( string variable, ConfigurationFlags flags ) /* THREAD-SAFE */ { string prefixedVariable = null; // // NOTE: If the variable name is null or empty, do nothing. // if (String.IsNullOrEmpty(variable)) { goto done; } // // NOTE: Try to set the variable name without the package name // prefix? // bool unprefixed = FlagOps.HasFlags( flags, ConfigurationFlags.Unprefixed, true); // // NOTE: Set the variable name prefixed by package name instead? // if (FlagOps.HasFlags(flags, ConfigurationFlags.Prefixed, true)) { prefixedVariable = String.Format( EnvVarFormat, EnvVarPrefix, variable); } #if CONFIGURATION // // NOTE: Does the caller want to remove the loaded AppSettings? // if (FlagOps.HasFlags(flags, ConfigurationFlags.AppSettings, true)) { // // NOTE: Try to unset the requested AppSettings value(s). // if (unprefixed) { ConfigurationOps.UnsetAppSetting(variable); } if (prefixedVariable != null) { ConfigurationOps.UnsetAppSetting(prefixedVariable); } } #endif // // NOTE: Does the caller want to remove the environment variables? // if (FlagOps.HasFlags(flags, ConfigurationFlags.Environment, true)) { // // NOTE: Try to unset the requested environment variable(s). // if (unprefixed) { CommonOps.Environment.UnsetVariable(variable); } if (prefixedVariable != null) { CommonOps.Environment.UnsetVariable(prefixedVariable); } } done: // // NOTE: Output diagnostic message about the configuration value // request. // if (DefaultVerbose || FlagOps.HasFlags(flags, ConfigurationFlags.Verbose, true)) { TraceOps.DebugTrace(String.Format( "UnsetValue: variable = {0}, prefixedVariable = {1}, " + "defaultVerbose = {2}, flags = {3}", FormatOps.WrapOrNull(variable), FormatOps.WrapOrNull(prefixedVariable), DefaultVerbose, FormatOps.WrapOrNull(flags)), typeof(GlobalConfiguration).Name, TracePriority.StartupDebug); } }
/////////////////////////////////////////////////////////////////////// public static string GetValue( string variable, ConfigurationFlags flags ) /* THREAD-SAFE */ { string prefixedVariable = null; // // NOTE: The default return value is null, which means that the // value is not available and/or not set. // string value = null; // // NOTE: If the variable name is null or empty, return the default // value (null) instead of potentially throwing an exception // later. // if (String.IsNullOrEmpty(variable)) { goto done; } // // NOTE: Try to get the variable name without the package name // prefix? // bool unprefixed = FlagOps.HasFlags( flags, ConfigurationFlags.Unprefixed, true); // // NOTE: Try to get the variable name prefixed by package name // first? // if (FlagOps.HasFlags(flags, ConfigurationFlags.Prefixed, true)) { prefixedVariable = String.Format( EnvVarFormat, EnvVarPrefix, variable); } // // NOTE: Does the caller want to check the environment variables? // if (FlagOps.HasFlags(flags, ConfigurationFlags.Environment, true)) { // // NOTE: Try the variable name prefixed by our package name // first? // if ((prefixedVariable != null) && (value == null)) { value = CommonOps.Environment.GetVariable(prefixedVariable); } // // NOTE: Failing that, just try for the variable name? // if (unprefixed && (value == null)) { value = CommonOps.Environment.GetVariable(variable); } } #if CONFIGURATION // // NOTE: Does the caller want to check the loaded AppSettings? // if (FlagOps.HasFlags(flags, ConfigurationFlags.AppSettings, true)) { // // NOTE: Try the variable name prefixed by our package name // first? // if ((prefixedVariable != null) && (value == null)) { value = ConfigurationOps.GetAppSetting(prefixedVariable); } // // NOTE: Failing that, just try for the variable name? // if (unprefixed && (value == null)) { value = ConfigurationOps.GetAppSetting(variable); } } #endif // // NOTE: If necessary, expand any contained environment variables. // if (!String.IsNullOrEmpty(value) && FlagOps.HasFlags(flags, ConfigurationFlags.Expand, true)) { value = CommonOps.Environment.ExpandVariables(value); } done: // // NOTE: Output diagnostic message about the configuration value // request. // if (DefaultVerbose || FlagOps.HasFlags(flags, ConfigurationFlags.Verbose, true)) { TraceOps.DebugTrace(String.Format( "GetValue: variable = {0}, prefixedVariable = {1}, " + "value = {2}, defaultVerbose = {3}, flags = {4}", FormatOps.WrapOrNull(variable), FormatOps.WrapOrNull(prefixedVariable), FormatOps.WrapOrNull(value), DefaultVerbose, FormatOps.WrapOrNull(flags)), typeof(GlobalConfiguration).Name, TracePriority.StartupDebug); } return(value); }