/// <summary>
 /// Writes to AppSettings by ConfigKeyValue object, will overwrite existing value.
 /// </summary>
 /// <param name="configObject"></param>
 public static void WriteSetting(ConfigKeyValue configObject)
 {
     try
     {
         ConfigurationManager.AppSettings[configObject.Key] = configObject.Value;
     }
     catch (Exception ex)
     {
         string[] errorMsgs = new string[] { "Cannot write value to configuration file.", ex.Message };
         Logger.WriteLine("ERROR", errorMsgs);
     }
 }
 /// <summary>
 /// Writes to AppSettings by ConfigKeyValue object, will overwrite existing value.
 /// </summary>
 /// <param name="configObject"></param>
 public static void WriteSetting(ConfigKeyValue configObject)
 {
     try
     {
         ConfigurationManager.AppSettings[configObject.Key] = configObject.Value;
     }
     catch (Exception ex)
     {
         string[] errorMsgs = new string[] { "Cannot write value to configuration file.", ex.Message };
         Logger.WriteLine("ERROR", errorMsgs);
     }
 }
Exemple #3
0
        private int ParseRecursive(Dictionary <object, object> rawConfig, Dictionary <ConfigKey, ConfigKeyItem> config, ConfigKey parentKey)
        {
            int itemsCount = 0;

            foreach (var pair in rawConfig)
            {
                if (_keyBuilder.TryCreate(pair.Key.ToString(), out var key))
                {
                    key = parentKey != null?parentKey.Merge(key) : key;

                    var value = pair.Value;

                    if (!key.VersionRange.InRange(_appVersion, _versionComparer))
                    {
                        continue;
                    }

                    if (!_segmentChecker.Check(key.Segment))
                    {
                        continue;
                    }

                    var keyExists = config.TryGetValue(key, out var keyItem);
                    var valueItem = keyItem as ConfigKeyValue;

                    if (value is Dictionary <object, object> dict)
                    {
                        if (valueItem != null)
                        {
                            throw new DynamicConfigException($"Key '{key.Key}' have different value types");
                        }

                        config[key] = new ConfigKeyItem
                        {
                            Key = key
                        };
                        itemsCount += ParseRecursive(dict, config, key);
                    }
                    else
                    {
                        if (keyExists)
                        {
                            if (valueItem == null)
                            {
                                throw new DynamicConfigException($"Key '{key.Key}' have different value types");
                            }

                            if (key.CompareTo(keyItem.Key) > 0)
                            {
                                valueItem.Key   = key;
                                valueItem.Value = value;
                            }
                        }
                        else
                        {
                            itemsCount++;
                            config[key] = new ConfigKeyValue
                            {
                                Key   = key,
                                Value = value
                            };
                        }
                    }
                }
            }

            return(itemsCount);
        }