基于.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 ...
随机推荐
- .NET:CLR via C# Thread Basics
A thread is a Windows concept whose job is to virtualize the CPU. Thread Overhead Thread kernel obje ...
- python接口自动化6-重定向(Location)
前言 某屌丝男A鼓起勇气向女神B打电话表白,女神B是个心机婊觉得屌丝男A是好人,不想直接拒绝于是设置呼叫转移给闺蜜C了,最终屌丝男A和女神闺蜜C表白成功了,这种场景其实就是重定向了. 一.重定向 1. ...
- 《学习opencv》笔记——矩阵和图像操作——cvInRange,cvInRangeS,cvInvert and cvMahalonobis
矩阵和图像的操作 (1)cvInRange函数 其结构 void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像 const CvArr* lowe ...
- Kafka broker配置介绍 (四)
这部分内容对了解系统和提高软件性能都有很大的帮助,kafka官网上也给出了比较详细的配置详单,但是我们还是直接从代码来看broker到底有哪些配置需要我们去了解的,配置都有英文注释,所以每一部分是干什 ...
- Lucene TFIDF打分公式
还没读TFIDFSimilarity的代码,读了一下lucene的文档,没有特复杂,感觉还是非常严谨的. 对于查询q和文档d,如果查询为纯token查询,套用向量空间模型(VSM),相似度度量使用余弦 ...
- 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset
bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...
- Windows 7目录
1. 用wubi安装的Ubuntu在重装Windows 7系统后,如何恢复(转) 2. Windows 7系统垃圾清理自写程序
- 我所遭遇过的游戏中间件---Redux
我所遭遇过的游戏中间件---Redux 一.关于Redux Substance Redux 是一款纹理处理软件加中间件,专门用于纹理生成和压缩.具其用户指南介绍,它能够对纹理集进行优化,可以将现有压缩 ...
- 我所遭遇过的游戏中间件--Apex
我所遭遇过的游戏中间件--Apex Apex是PhysX的扩展中间件,它是在PhysX的基础上封装了一层.用于实现布料,粒子,破碎这三种物理效果.我只研究其布料处理.使用Apex做物理最大的好处是:它 ...
- elimination-game
https://leetcode.com/problems/elimination-game/ // 一行代码就可以,不过原理有些复杂 // https://discuss.leetcode.com/ ...