保存或讀取配置信息

 class Config
    {
        public static string ReadParam(string key)
        {
            string result = string.Empty;
            try
            {
                var appSettings = ConfigurationManager.AppSettings;
                result = appSettings[key] ?? string.Empty;
            }
            catch(Exception ex)
            {
                Log.Add(ex.Message);               
            }
            return result;
        }
        public static void SaveParam(string key,string value)
        {            
            try
            {
                Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = cfg.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                cfg.Save(ConfigurationSaveMode.Full);
                ConfigurationManager.RefreshSection(cfg.AppSettings.SectionInformation.Name);
            }
            catch(Exception ex)
            {
                Log.Add(ex.Message);
                throw new Exception(ex.Message);
            }
             
        }
    }
原文地址:https://www.cnblogs.com/yagzh2000/p/14344584.html