C#读取web.config里边appSettings的配置

前段时间做一个收发邮件的模块,收件人邮箱要配置一下为了方便后期更改就把收件人邮箱配置在web.config里边在此记录一下。

添加一个帮助类,直接搜可以找的到

using System;
using System.Configuration;
using System.Globalization;

namespace Common
{
  public static class Util
    {
        #region Public Methods

        /// <summary>
        /// Gets configuration value for the specified key (null if key does not exist)
        /// </summary>
        /// <param name="key">Configuration key</param>
        /// <returns></returns>
        public static string GetConfigValue(
            string key
            )
        {
            return ConfigurationManager.AppSettings.Get(key);
        }


        public static string GetConnectionString(
            string name
            )
        {
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;
        }

        /// <summary>
        /// Gets configuration value for the specified key (0 if key does not exist)
        /// </summary>
        /// <param name="key">Configuration key</param>
        /// <returns></returns>
        public static int GetConfigIntValue(
            string key
            )
        {
            string value = GetConfigValue(key);
            int intValue;

            int.TryParse(value,
                         NumberStyles.Integer,
                         CultureInfo.InvariantCulture,
                         out intValue);

            return intValue;
        }

        /// <summary>
        /// Gets configuration value for the specified key (uses default value if key does not exist)
        /// </summary>
        /// <param name="key">Configuration key</param>
        /// <param name="defaultValue">Default value if key does not exist</param>
        /// <returns></returns>
        public static string GetConfigValue(
            string key,
            string defaultValue
            )
        {
            string value = GetConfigValue(key);

            if (value == null)
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Gets configuration value for the specified key (uses default value if key does not exist)
        /// </summary>
        /// <param name="key">Configuration key</param>
        /// <param name="defaultValue">Default value if key does not exist</param>
        /// <returns></returns>
        public static int GetConfigIntValue(
            string key,
            int defaultValue
            )
        {
            string value = GetConfigValue(key);
            int intValue;

            if (!int.TryParse(value,
                              NumberStyles.Integer,
                              CultureInfo.InvariantCulture,
                              out intValue))
            {
                intValue = defaultValue;
            }

            return intValue;
        }

        /// <summary>
        /// Gets configuration value for the specified key (uses default value if key does not exist)
        /// </summary>
        /// <param name="key">Configuration key</param>
        /// <param name="defaultValue">Default value if key does not exist</param>
        /// <returns></returns>
        public static bool GetConfigBooleanValue(
            string key,
            bool defaultValue
            )
        {
            string value = GetConfigValue(key);
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }

            return value.Trim().Equals("true", StringComparison.OrdinalIgnoreCase) ||
                   value.Trim().Equals("yes", StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Updates a specific app setting in the config
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        /// I noticed that nothing seemed to be written to the configuration file while debugging within Visual Studio, 
        /// but once I published the application it worked as expected.
        /// it’s because when you make that call vs is editing the vshost.exe.config file in the debug directory not the app.config in your project.
        public static bool UpdateAppSetting(string name, string value)
        {
            try
            {
                Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                oConfig.AppSettings.Settings[name].Value = value;
                oConfig.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool IsTest { get { return true; } }
  #endregion
}
}

在web.config里边

<appSettings>
		
		<!-- 密送邮件-->
		<add key="MailBCC" value="2669420437@qq.com" />
		<!-- 抄送邮件-->
		<add key="MailCC" value="2669420437@qq.com" />
		<!-- 报错邮件收件人-->
		<add key="Mailmassage" value="2669420437@qq.com" />
</appSettings>

在控制器里边

MailCC = Util.GetConfigValue("MailCC"); 
MailBCC = Util.GetConfigValue("MailBCC");

这样改邮箱地址直接改web.config里边的地址就可以了

除了配置邮箱配置其他的东西也可以这样获取。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值