zoukankan      html  css  js  c++  java
  • .net获取/修改配置文件/web.config

     配置实体类 (属性名称与配置项一致即可):

     public class WechatConfig
        {
       public string A{ get; set; }
      public string B{ get; set; }
      public string C{ get; set; }
    
    }

    获取配置信息

    var configInfo = new WechatConfig();
                Configuration _Config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                AppSettingsSection _AppSettings = _Config.AppSettings;
    
                var props = configInfo.GetType().GetProperties();
    
                System.Reflection.PropertyInfo[] properties = configInfo.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (System.Reflection.PropertyInfo item in properties)
                {
                    string name = item.Name;
                    var config = _AppSettings.Settings[name];
                    item.SetValue(configInfo, config.Value);
                }

    设置配置信息

    Configuration _Config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                AppSettingsSection _AppSettings = _Config.AppSettings;
                var props = param.GetType().GetProperties();
                foreach (var item in props)
                {
                    var key = item.Name;
                    var value = param.GetType().GetProperty(key).GetValue(param, null);
                    if (_AppSettings.Settings[key] != null)
                        _AppSettings.Settings.Remove(key);
                    _AppSettings.Settings.Add(key, value.ToString());
                }
                _Config.Save();

    注意:

     如果有些配置项是在Application_Start加载使用的话,设置配置信息后需要重启服务才会生效,很明显十分不方便。

     所以最好封装需要使用配置数据的功能,在设置完配置项后调用一次即可

    例如:使用盛派SDK需要在Application_Start全局注册一下微信信息,当我通过以上方法修改了微信配置时,再次调用一下我封装的盛派注册微信信息方法即可即时更新配置数据。

  • 相关阅读:
    oracle转义用单引号
    【转】plsql 永久注册码适用个版本
    winform datagridview某一列设为自动宽度
    Allow windows service to "Interact with desktop"
    Format a Hard Drive in Csharp C#格式化总结
    Lib New
    大嫂的HTML
    ASP.NET 分页控件
    linux搭建常用命令(运行jar,查看进程)
    如何用navicat连接linux服务器上的mysql以及重启服务
  • 原文地址:https://www.cnblogs.com/ncellit/p/12689765.html
Copyright © 2011-2022 走看看