Example #1
0
 /// <summary>
 /// retrieve setting
 /// </summary>
 public static string GetSetting(string settingId)
 {
     if (IsLoadedConfigFile && SettingList.ContainsKey(settingId))
     {
         return(SettingList [settingId]);
     }
     else
     {
         return(string.Empty);
     }
 }
        /// <summary>
        /// 设置指定setion-Key的配置值,若Key不存在,将自动添加Key并设置值
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="keyName"></param>
        /// <param name="value"></param>
        private void SetKeyValue(string sectionName, object keyName, object value)
        {
            LastErrMsg = string.Empty;
            try
            {
                //string sectionName = string.Empty;
                bool isAddOk = false;
                ConcurrentDictionary <object, object> oneSection = null;
                if (SettingList.ContainsKey(sectionName))
                {
                    oneSection = SettingList[sectionName];
                }
                else
                {
                    oneSection = new ConcurrentDictionary <object, object>();
                    isAddOk    = SettingList.TryAdd(sectionName, oneSection);
                    if (!isAddOk)
                    {
                        LastErrMsg = string.Format("Add section:{0} failed!", sectionName);
                        return;
                    }
                }

                if (oneSection.ContainsKey(keyName))
                {
                    oneSection[keyName] = value;
                }
                else
                {
                    isAddOk = oneSection.TryAdd(keyName, value);
                    if (!isAddOk)
                    {
                        LastErrMsg = string.Format("Add name:{0} value:{1}  failed!", keyName, value);
                    }
                }
            }
            catch (Exception e)
            {
                LastErrMsg = string.Format("Set the value:{0} of {1} failed!  {2}", value, keyName, ConfigHelper.GetExceptionInfo(e));
            }
        }
        /// <summary>
        /// 根据section-Key获取找到到配置值,
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="keyName"></param>
        /// <returns></returns>
        private object GetValueByKey(string sectionName, object keyName)
        {
            LastErrMsg = string.Empty;
            try
            {
                object valueObj = null;
                bool   isAddOk  = false;
                //string sectionName = string.Empty;
                ConcurrentDictionary <object, object> oneSection = null;

                //如果在正式列表中找到,则直接返回对应到配置值
                if (SettingList.ContainsKey(sectionName))
                {
                    oneSection = SettingList[sectionName];
                    if (oneSection.ContainsKey(keyName))
                    {
                        valueObj = oneSection[keyName];
                        return(valueObj);
                    }
                }


                //如果在加载的正式配置列表中没有,但在默认配置项列表中存在,则取默认值,并将默认值加入到正式列表中用于后续保存
                if (DefaultSettingList.ContainsKey(sectionName))
                {
                    oneSection = DefaultSettingList[sectionName];
                    if (oneSection.ContainsKey(keyName))
                    {
                        valueObj = oneSection[keyName];


                        if (SettingList.ContainsKey(sectionName))
                        {
                            oneSection = SettingList[sectionName];
                        }
                        else
                        {
                            oneSection = new ConcurrentDictionary <object, object>();
                            isAddOk    = SettingList.TryAdd(sectionName, oneSection);
                            if (!isAddOk)
                            {
                                LastErrMsg = string.Format("Can not find the value of {0} and add section:{0} failed!", keyName, sectionName);
                                return(null);
                            }
                        }
                        isAddOk = oneSection.TryAdd(keyName, valueObj);
                        if (!isAddOk)
                        {
                            LastErrMsg = string.Format("Can not find the value of {0} and add failed!", keyName);
                            return(null);
                        }
                        return(valueObj);
                    }
                }
            }
            catch (Exception e)
            {
                LastErrMsg = string.Format("get the value of {0} failed!  {1}", keyName, ConfigHelper.GetExceptionInfo(e));
                return(null);
            }
            return(null);
        }