配置文件的读取添加webconfig
webconfig.xml的配置文件内容挺丰富的,在这篇文章里笔者只对AppSettings这个节点进行配置文件读取和添加
public class ConfigurationRef
{
/// <summary>
/// 根据键值获取配置文件
/// </summary>
/// <param name="key">键值</param>
/// <returns></returns>
public static string GetConfig(string key)
{
string val = string.Empty;
if (WebConfigurationManager.AppSettings.AllKeys.Contains(key))
val = WebConfigurationManager.AppSettings[key];
return val;
} /// <summary>
/// 获取所有配置文件
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> GetConfig()
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (string key in WebConfigurationManager.AppSettings.AllKeys)
dict.Add(key, WebConfigurationManager.AppSettings[key]);
return dict;
} /// <summary>
/// 根据键值获取配置文件
/// </summary>
/// <param name="key">键值</param>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public static string GetConfig(string key, string defaultValue)
{
string val = defaultValue;
if (WebConfigurationManager.AppSettings.AllKeys.Contains(key))
val = WebConfigurationManager.AppSettings[key];
if (val == null)
val = defaultValue;
return val;
} /// <summary>
/// 写配置文件,如果节点不存在则自动创建
/// </summary>
/// <param name="key">键值</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool SetConfig(string key, string value)
{
try
{
Configuration conf = WebConfigurationManager.OpenWebConfiguration("~");
if (!conf.AppSettings.Settings.AllKeys.Contains(key))
conf.AppSettings.Settings.Add(key, value);
else
conf.AppSettings.Settings[key].Value = value;
conf.Save();
return true;
}
catch { return false; }
} /// <summary>
/// 写配置文件(用键值创建),如果节点不存在则自动创建
/// </summary>
/// <param name="dict">键值集合</param>
/// <returns></returns>
public static bool SetConfig(Dictionary<string, string> dict)
{
try
{
if (dict == null || dict.Count == )
return false;
Configuration conf = WebConfigurationManager.OpenWebConfiguration("~");//ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (string key in dict.Keys)
{
if (!conf.AppSettings.Settings.AllKeys.Contains(key))
conf.AppSettings.Settings.Add(key, dict[key]);
else
conf.AppSettings.Settings[key].Value = dict[key];
}
conf.Save();
return true;
}
catch { return false; }
}
}
代码贴出来了,其实也没什么好解释的,相信大家都能看懂
配置文件的读取添加webconfig的更多相关文章
- System.ConfigurationManager类用于对配置文件的读取
http://blog.csdn.net/ligenyingsr/article/details/54095986 System.ConfigurationManager类用于对配置文件的读取.其具有 ...
- 【APS.NET Core】- Json配置文件的读取
在项目目录下有个 appsettings.json ,我们先来操作这个文件.在appsettings.json中添加以下内容: { "Logging": { "LogLe ...
- properties配置文件的读取和写入
/** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...
- 解决mysql配置文件my.cnf添加max_connections不生效
问题描述: 最新为了方便测试,通过mysql官方指定的yum源安装了mysql5.6.40,在向mysql的配置文件my.cnf添加max_connections=3600后,重启mysql后发现不生 ...
- springcloud的配置文件的读取顺序
SpringBoot默认支持properties和YAML两种格式的配置文件.前者格式简单,但是只支持键值对.如果需要表达列表,最好使用YAML格式.SpringBoot支持自动加载约定名称的配置文件 ...
- Springboot 之 自定义配置文件及读取配置文件
本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...
- Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)
注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED = ABAP_AS_WITH_P ...
- Feign从配置文件中读取url
Feign的url和name都是可配置的,就是从配置文件中读取的属性值,然后用占位符引用就可以了: ${rpc.url} @FeignClient(name = "me", url ...
- IT项目中使用 json格式数据 保存项目配置信息, 在配置文件再读取json文件的内容进行赋值
json格式小巧玲珑,适合做配置文件,特别是大型项目中, 可以将配置信息分类保存到不同的json文件中, 然后再在配置文件中读取配置文件的数据进行赋值, 这里以python为例进行说明: 假设在you ...
随机推荐
- C#中打日志导出日志到txt文本
/// <summary> /// 打日志 /// </summary> /// <param name="log"></param> ...
- 去掉firefox点击按钮时的虚线边框
去掉火狐里面点击按钮时候的虚线边框 button::-moz-focus-inner, input[type="reset"]::-moz-focus-inner, input[t ...
- ios 添加多个target 管理 多个版本文件
1. 添加一个Target 这里是添加一个Test 项目 这里添加新的target Test与Release 也是同上的操作
- jQuery + css 公告从左往右滚动
$(function() { // 公告滚动 $(".notice-content").textScroll(); }); /** * 从右往左滚动文字 * @returns {u ...
- css多行文本居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 转:说说JSON和JSONP
前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...
- Canvas的补充
一.Canvas.save()作用:让后续的操作就好像在一个新图层上. 二.Canvas.restore():让之前save()产生的图层全部合并. 三.Canvas.translate():将原点( ...
- WDLINUX (Centos5.8) 安装 bcmath
环境 centos5.8 php5.2.17 因为wdos 集成的php5.2.17为精简版,并未包含php52-bcmath扩展. 所以先下载完整php5.2.17源码包 wget -c http: ...
- python实现zabbix_sender的socket通信代码样例
sk = socket.socket() sk.connect(self.ip_port) sk.settimeout(5) sk.sendall(b'ZBXD\x01') sk.sendall(b' ...
- $.each与$(data).each区别
在前端使用使用JQuery解析Json数据时,在遍历数组或者对象数据时,经常使用的函数为each.发现此函数有两种形式: $.each $(data).each 所达到的效果是一样的,使用方法的有一些 ...