码迷,mamicode.com
首页 > 移动开发 > 详细

[C#]appSettings节点读取与修改

时间:2014-09-01 12:07:53      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   文件   2014   

关键代码:

        public static string GetAppSettingsValue(string key)
        {
            ConfigurationManager.RefreshSection("appSettings");
            return ConfigurationManager.AppSettings[key] ?? string.Empty;
        }
        public static bool UpdateAppSettings(string key, string value)
        {
            var _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (!_config.HasFile)
            {
                throw new ArgumentException("程序配置文件缺失!");
            }
            KeyValueConfigurationElement _key = _config.AppSettings.Settings[key];
            if (_key == null)
                _config.AppSettings.Settings.Add(key, value);
            else
                _config.AppSettings.Settings[key].Value = value;
            _config.Save(ConfigurationSaveMode.Modified);
            return true;
        }

希望有所帮助!bubuko.com,布布扣

[C#]appSettings节点读取与修改

标签:style   blog   http   color   os   io   ar   文件   2014   

原文地址:http://www.cnblogs.com/Yan-Zhiwei/p/3948729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!