using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace ChuPiao.Common.Utils
{
    /// <summary>
    /// 往app.config中的AppSetting里面添加或修改键值对的类
    /// </summary>
    public class AppConfigUtil
    {
        private static System.Configuration.Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        /// <summary>
        /// 保存设置到AppSetting,有就修改，没有就新增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SaveConfig(string key, string value)
        {
            if (HasKey(key))
            {
                _config.AppSettings.Settings[key].Value = value;
            }
            else
            {
                _config.AppSettings.Settings.Add(key, value);
            }
            _config.Save();
        }


        private static bool HasKey(string key)
        {
            foreach (string Key in _config.AppSettings.Settings.AllKeys)
            {
                if (Key == key)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
