首页 > 移动平台 > 详细

c# 配置文件App.config操作类库

时间:2016-09-19 11:34:49      阅读:193      评论:0      收藏:0      [点我收藏+]

 

public class ConfigOperator
    {
        #region 从配置文件获取Value
        /// <summary>
        /// 从配置文件获取Value
        /// </summary>
        /// <param name="key">配置文件中key字符串</param>
        /// <returns></returns>
        public static string GetValueFromConfig(string key)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //获取AppSettings的节点 
                AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
                return appsection.Settings[key].Value;
            }
            catch
            {
                return "";
            }
        }
        #endregion

        #region 设置配置文件
        /// <summary>
        /// 设置配置文件
        /// </summary>
        /// <param name="key">配置文件中key字符串</param>
        /// <param name="value">配置文件中value字符串</param>
        /// <returns></returns>
        public static bool SetValueFromConfig(string key, string value)
        {
            try
            {
                //打开配置文件 
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //获取AppSettings的节点 
                AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
                appsection.Settings[key].Value = value;
                config.Save();

                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

 

c# 配置文件App.config操作类库

原文:http://www.cnblogs.com/xuxiaoshuan/p/5884406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!