基于.net 的加载自定义配置-误操作
有时候 需要 将程序加载自定义的配置文件,除了自己写解析xml文件。内置的ConfigutionManager对象 是个不错的选项。
按照 app.config 的方式,做一个副本。然后从你的配置文件中,加载指定的配置 键值对!
//方法1-可以读取任意位置的xml文件
ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
filemap.ExeConfigFilename = QuartzConfigPath; //filePath;
var config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
// 方法2 -只能读取当前应用程序域中 根目录下的文件:注意exe的含义
//var config = ConfigurationManager.OpenExeConfiguration(QuartzConfigPath);
[csharp] view plain copy
public class ConfigureAppConfig
{
//静态构造,不能实例化
static ConfigureAppConfig() { } /**//// <summary>
/// 获取AppSettings配置节中的Key值
/// </summary>
/// <param name="keyName">Key's name</param>
/// <returns>Key's value</returns>
public static string GetAppSettingsKeyValue(string keyName)
{
return ConfigurationManager.AppSettings.Get(keyName);
} /**//// <summary>
/// 获取ConnectionStrings配置节中的值
/// </summary>
/// <returns></returns>
public static string GetConnectionStringsElementValue()
{
ConnectionStringSettings settings =System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];
return settings.ConnectionString;
} /**//// <summary>
/// 保存节点中ConnectionStrings的子节点配置项的值
/// </summary>
/// <param name="elementValue"></param>
public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
} /**//// <summary>
/// 判断appSettings中是否有此项
/// </summary>
private static bool AppSettingsKeyExists(string strKey, Configuration config)
{
foreach (string str in config.AppSettings.Settings.AllKeys)
{
if (str == strKey)
{
return true;
}
}
return false;
} /**//// <summary>
/// 保存appSettings中某key的value值
/// </summary>
/// <param name="strKey">key's name</param>
/// <param name="newValue">value</param>
public static void AppSettingsSave(string strKey, string newValue)
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (AppSettingsKeyExists(strKey, config))
{
config.AppSettings.Settings[strKey].Value = newValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
} // 如果你的程序是对其它程序的配置文件进行操作,代码如下:
[csharp] view plain copy
ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
filemap.ExeConfigFilename = filePath;//配置文件路径
config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
if (AppSettingsKeyExists("Refresh", config))
{
config.AppSettings.Settings["Refresh"].Value = M_TimeRead.ToString();
} if (AppSettingsKeyExists("MachineNo", config))
{
config.AppSettings.Settings["MachineNo"].Value = M_MachineNo; }
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = M_ConnectionString;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
数据库字符串加密
ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
filemap.ExeConfigFilename = Application.ExecutablePath + ".Config"; //filePath;
config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
//指定我所要的节点
ConfigurationSection section = config.ConnectionStrings;
if ((section.SectionInformation.IsProtected == false) && (section.ElementInformation.IsLocked == false))
{
//制定节点加密
section.SectionInformation.ProtectSection(protect);
//即使没有修改也保存设置
section.SectionInformation.ForceSave = true;
//配置文件内容保存到xml
config.Save(ConfigurationSaveMode.Full);
}
- public class ConfigureAppConfig
- {
- //静态构造,不能实例化
- static ConfigureAppConfig() { } /**//// <summary>
- /// 获取AppSettings配置节中的Key值
- /// </summary>
- /// <param name="keyName">Key's name</param>
- /// <returns>Key's value</returns>
- public static string GetAppSettingsKeyValue(string keyName)
- {
- return ConfigurationManager.AppSettings.Get(keyName);
- } /**//// <summary>
- /// 获取ConnectionStrings配置节中的值
- /// </summary>
- /// <returns></returns>
- public static string GetConnectionStringsElementValue()
- {
- ConnectionStringSettings settings =System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];
- return settings.ConnectionString;
- } /**//// <summary>
- /// 保存节点中ConnectionStrings的子节点配置项的值
- /// </summary>
- /// <param name="elementValue"></param>
- public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)
- {
- System.Configuration.Configuration config =
- ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("connectionStrings");
- } /**//// <summary>
- /// 判断appSettings中是否有此项
- /// </summary>
- private static bool AppSettingsKeyExists(string strKey, Configuration config)
- {
- foreach (string str in config.AppSettings.Settings.AllKeys)
- {
- if (str == strKey)
- {
- return true;
- }
- }
- return false;
- } /**//// <summary>
- /// 保存appSettings中某key的value值
- /// </summary>
- /// <param name="strKey">key's name</param>
- /// <param name="newValue">value</param>
- public static void AppSettingsSave(string strKey, string newValue)
- {
- System.Configuration.Configuration config =
- ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (AppSettingsKeyExists(strKey, config))
- {
- config.AppSettings.Settings[strKey].Value = newValue;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");
- }
- }
- }
// 如果你的程序是对其它程序的配置文件进行操作,代码如下:
- ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
- filemap.ExeConfigFilename = filePath;//配置文件路径
- config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
- if (AppSettingsKeyExists("Refresh", config))
- {
- config.AppSettings.Settings["Refresh"].Value = M_TimeRead.ToString();
- } if (AppSettingsKeyExists("MachineNo", config))
- {
- config.AppSettings.Settings["MachineNo"].Value = M_MachineNo; }
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");
- config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = M_ConnectionString;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("connectionStrings");
- 数据库字符串加密
- ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
- filemap.ExeConfigFilename = Application.ExecutablePath + ".Config"; //filePath;
- config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
- //指定我所要的节点
- ConfigurationSection section = config.ConnectionStrings;
- if ((section.SectionInformation.IsProtected == false) && (section.ElementInformation.IsLocked == false))
- {
- //制定节点加密
- section.SectionInformation.ProtectSection(protect);
- //即使没有修改也保存设置
- section.SectionInformation.ForceSave = true;
- //配置文件内容保存到xml
- config.Save(ConfigurationSaveMode.Full);
- }
基于.net 的加载自定义配置-误操作的更多相关文章
- spring boot加载自定义配置
1.通过@Value 配置文件中 wechat: ssh: host: 192.0.1.1 port: 22 加载类 @Component @Data public class SftpConfig ...
- [Yii2.0] 以Yii 2.0风格加载自定义类或命名空间 [配置使用Yii2 autoloader]
Yii 2.0最显著的特征之一就是引入了命名空间,因此对于自定义类的引入方式也同之前有所不同.这篇文章讨论一下如何利用Yii 2.0的自动加载机制,向系统中引入自定义类和命名空间.本文旨在抛砖引玉,如 ...
- asp.net core重新加载应用配置
asp.net core重新加载应用配置 Intro 我把配置放在了数据库或者是Redis里,配置需要修改的时候我要直接修改数据库,然后调用一个接口去重新加载应用配置,于是就尝试写一个运行时重新加载配 ...
- 写个重新加载 ocelot 配置的接口
写个重新加载 ocelot 配置的接口 Intro 我们想把 ocelot 的配置放在自己的存储中,放在 Redis 或者数据库中,当修改了 Ocelot 的配置之后希望即时生效,又不想在网关这边定时 ...
- SpringBoot系列——加载自定义配置文件
前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...
- atitit.动态加载数据库配置in orm hibernate mybatis
atitit.动态加载数据库配置in orm 1. 动态加载数据库配置的优点::: 1 1.1. 组合多个配置文件... 1 1.2. 连接多个数据库 1 2. 基本的流程:::getCfg内存对象, ...
- 自动化测试-14.selenium加载FireFox配置
前言 有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile ...
- 2.14 加载Firefox配置
2.14 加载Firefox配置(略,已在2.1.8讲过,请查阅2.1.8节课) 回到顶部 2.14-1 加载Chrome配置 一.加载Chrome配置chrome加载配置方法,只需改下面一个地方,u ...
- PIE SDK加载自定义服务数据
1.功能简介 自定义服务数据,将符合要求的矢量数据和栅格数据集等数据以服务的方式发布,将数据存储在某服务器中,在有网络的情况下可以根据URL就可以访问,比较常见的服务数据类型的有ArcGIS Serv ...
随机推荐
- powerdesigner 设置字段显示comment注释
在Columns标签下,一排按钮中找到这个按钮:Customize Columns and Filter
- use of undeclared identifier 'xxxxxxx方法名'
在*.m文件中,编写一个方法,出现了 use of undeclared identifier 'xxxx方法名'. 遇到这种情况: 首先要看,*.h 文件是否定义了该方法. 其次,要检查一下,方 ...
- python文本 maketrans和translate
python文本 maketrans和translate 场景: 过滤字符串的某些字符,我们从例子出发 >>> tb=str.maketrans ('abc','123') & ...
- .NET:异常以及异常处理框架探析(转载)
概述 一般情况下,企业级应用都对应着复杂的业务逻辑,为了保证系统的健壮,必然需要面对各种系统业务异常和运行时异常. 不好的异常处理方式容易造成应用程序逻辑混乱,脆弱而难于管理.应用程序中充斥着零散的异 ...
- Iptables静态防火墙基础教程
文章目录检查Iptables是否安装Iptables相关的文件配置Iptables规则自定义规则保存规则 Iptables对于刚入门Linux的新手都比较难理解和配置.但是如果你掌握了其中的诀窍,你就 ...
- Android之MVC——Model通知View去更新(实用)
下面两段标红加深的代码是重点: import android.app.Activity; import android.os.Bundle; import android.view.View; imp ...
- acd Convex(求面积)
Problem Description We have a special convex that all points have the same distance to origin point. ...
- Tengine zabbix 监控
Tengine 配置 在http 段下新增以下配置 req_status_zone server_stat "$host" 3M; server { listen 9008; lo ...
- Android中intent如何传递自定义数据类型
转载自:http://www.cnblogs.com/GoAhead/archive/2012/07/16/2593868.html 大家好,好久不见,今天要给大家讲一下Android中Intent中 ...
- OkHttp 官方Wiki之【使用案例】
原文位置:https://github.com/square/okhttp/wiki/Recipes Recipes 食谱/知识点清单 We've written some recipes that ...