C# AppSettings使用

本文介绍如何使用C#的AppConfigHelper类库高效管理应用程序配置,包括读取不同数据类型的AppSetting,以及修改App.config文件中的配置。通过实例演示了如何通过代码访问和设置AppSettings,并强调了配置文件与可执行程序名称的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作为C#参数配置读取,必不可少的就需要用到System.Configuration类库,之前经常手动配置App.Config进行参数配置,然后使用

ConfigurationManager.AppSettings[appSettingKey];

来进行参数配置读取,其实这个类和INI文件类似,也是可以进行创建的,写入初始值的,AppConfigHelper见下面代码,注意:编译完成后的.Config文件名称需要跟可执行程序名称一致,否者读取不到对应的参数值

    /// <summary>
    /// 应用程序配置文件访问辅助类
    /// </summary>
    public class AppConfigHelper
    {
        /// <summary>
        /// 读取string配置项
        /// </summary>
        /// <param name="appSettingKey">AppSetting配置项的Key值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回对应配置项的string类型value值</returns>
        public static string GetAppSettingValue(string appSettingKey, string defaultValue)
        {
            string result = ConfigurationManager.AppSettings[appSettingKey];
            if (String.IsNullOrEmpty(result))
            {
                return defaultValue;
            }
            return result;
        }

        /// <summary>
        /// 读取bool配置项
        /// </summary>
        /// <param name="appSettingKey">AppSetting配置项的Key值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回对应配置项的bool类型value值</returns>
        public static bool GetAppSettingValue(string appSettingKey, bool defaultValue)
        {
            string result = ConfigurationManager.AppSettings[appSettingKey];
            if (String.IsNullOrEmpty(result))
            {
                return defaultValue;
            }
            bool boolResult = false;
            if (bool.TryParse(result, out boolResult))
            {
                return boolResult;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 读取int配置项
        /// </summary>
        /// <param name="appSettingKey">AppSetting配置项的Key值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回对应配置项的int类型value值</returns>
        public static int GetAppSettingValue(string appSettingKey, int defaultValue)
        {
            string result = ConfigurationManager.AppSettings[appSettingKey];
            if (String.IsNullOrEmpty(result))
            {
                return defaultValue;
            }
            int intResult = 0;
            if (int.TryParse(result, out intResult))
            {
                return intResult;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 读取double类型配置项
        /// </summary>
        /// <param name="appSettingKey">AppSetting配置项的Key值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回对应配置项的double类型value值</returns>
        public static double GetAppSettingValue(string appSettingKey, double defaultValue)
        {
            string result = ConfigurationManager.AppSettings[appSettingKey];
            if (String.IsNullOrEmpty(result))
            {
                return defaultValue;
            }
            double doubleResult = 0.0;
            if (double.TryParse(result, out doubleResult))
            {
                return doubleResult;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 修改App.config中AppSetttings中的配置项
        /// </summary>
        /// <param name="name">要修改的配置项的名称</param>
        /// <param name="value">要修改的配置项的值</param>
        /// <returns></returns>
        public static bool SetAppSettings(string name, string value, bool _ismodify = false)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//当前应用程序的配置文件
                if (config != null)
                {
                    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
                    if (appSettings.Settings.AllKeys.Contains(name))
                    {
                        if (_ismodify)
                            appSettings.Settings[name].Value = value;
                    }
                    else
                    {
                        appSettings.Settings.Add(name, value);
                    }
                    config.Save(); //保存配置文件
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("修改app.config配置{0}的值为{1}异常:{2}", name, value, ex.Message));
                return false;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冀石程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值