Web读取指定的config文件的内容
需求:
什么时候会用到动态改变Web.config内的值?
在Web.config定义了一个全局设置值A,因为程序运行中满足了某个条件,要将A的值改变
Web.config中定义:
<appSettings>
<add key="IsChangeDb" value="false"/>
</appSettings>
获取Web.config中指定配置:
//获取web.config中 定义在appSetting中定义的配置
var item = ConfigurationManager.AppSettings["IsChangeDb"];
//根据我自己的需要转换成bool值
bool isChangeDb = string.IsNullOrEmpty(ConfigurationManager.AppSettings["IsChangeDb"]) ? false : bool.Parse(ConfigurationManager.AppSettings["IsChangeDb"]);
代码中更改Web.config中指定配置:
Configuration config=System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
config.AppSettings.Settings.Remove("IsChangeDb");
config.AppSettings.Settings.Add("IsChangeDb", "true");
config.Save();
参考资料:
http://stackoverflow.com/questions/719928/how-do-you-modify-the-web-config-appsettings-at-runtime
出处:https://blog.csdn.net/qq_32452623/article/details/53580093
============================================================================
asp.net2.0新添加了对web.config直接操作的功能。开发的时候有可能用到在web.config里设置配置文件,其实是可以通过程序来设置这些配置节的。
asp.net2.0需要添加引用:
using System.Web.Configuration;
web.config里的配置节:
Code
<appSettings>
<add key="FilePath" value="g:\Test\WebConfigManager\Upload\" />
<add key="p" value="g:\" />
</appSettings>
(1)读
string filepath = ConfigurationManager.AppSettings["FilePath"];
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
AppSettingsSection app = config.AppSettings;
app.Settings.Add("p", "p:\\");
config.Save(ConfigurationSaveMode.Modified);
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
AppSettingsSection app = config.AppSettings;
app.Settings["p"].Value = @"g:\";
config.Save(ConfigurationSaveMode.Modified);
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("p");
config.Save(ConfigurationSaveMode.Modified);
出处:https://www.cnblogs.com/xxtkong/archive/2011/10/14/2211793.html
==============================================================================
通过从网上的了解,和学习,我们看到ConfigurationManager.OpenMappedExeConfiguration这个方法可以用于打开指定的配置文件,那么看看我们用它来做一些事情吧,下面看代码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration; namespace PVG.Lib.Configs
{
public class WebConfigHelper
{
/// <summary>
/// 是否加密连接字符串
/// </summary>
public bool IsEncryptionConnection { get; set; } private Configuration config = null;
public Configuration Configuration
{
get { return config; }
set { config = value; }
} public WebConfigHelper()
{
config = WebConfigurationManager.OpenWebConfiguration("~");
} /// <summary>
/// 读取ConnectionStrings
/// </summary>
/// <param name="ConnName"></param>
/// <returns></returns>
public string GetConnectionStrings(string ConnName)
{
string res = "";
if (config != null && config.ConnectionStrings.ConnectionStrings[ConnName] != null)
res = config.ConnectionStrings.ConnectionStrings[ConnName].ConnectionString;
return res;
} public string SetConnectionStrings(string ConnName, string ConnValue)
{
return SetConnectionStrings(ConnName, ConnValue, "");
} public string SetConnectionStrings(string ConnName, string ConnValue, string providerName)
{
if (config != null)
{
if (config.ConnectionStrings.ConnectionStrings[ConnName] != null)
config.ConnectionStrings.ConnectionStrings[ConnName].ConnectionString = ConnValue;
else
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(ConnName, ConnValue, providerName));
config.Save(ConfigurationSaveMode.Modified);
}
if (IsEncryptionConnection)
encryptionConn();//加密配置项
return GetConnectionStrings(ConnName); } public string GetAppSettings(string keyName)
{
string res = "";
if (config != null && config.AppSettings.Settings[keyName] != null)
res = config.AppSettings.Settings[keyName].Value;
return res;
} public string SetAppSettings(string keyName, string keyValue)
{
if (config != null)
{
if (config.AppSettings.Settings[keyName] != null)
config.AppSettings.Settings[keyName].Value = keyValue;
else
config.AppSettings.Settings.Add(keyName, keyValue);
config.Save(ConfigurationSaveMode.Modified);
}
return GetAppSettings(keyName); } private void encryptionConn()
{ ConfigurationSection connectionSection = config.GetSection("connectionStrings");
if (connectionSection != null)
{
connectionSection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
config.Save();
}
} }
}
Web读取指定的config文件的内容的更多相关文章
- WinForm读取指定的config文件的内容
config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...
- php读取指定结束指针文件内容
fopen操作时文件读取开始指针位于文件开始部分, fseek 以指定文件大小以及开始指针位置确定结束指针位置 具体案例: <?php//打开文件流,fopen不会把文件整个加载到内存$f = ...
- 【ASP.NET 进阶】定时执行任务实现 (定时读取和修改txt文件数字内容,无刷新显示结果)
现在有很多网站或系统需要在服务端定时做某件事情,如每天早上8点半清理数据库中的无效数据等等,Demo 具体实现步骤如下: 0.先看解决方案截图 1.创建ASP.NET项目TimedTask,然后新建一 ...
- PCL点云库中怎样读取指定的PCD文件,又一次命名,处理后保存到指定目录
我一直想把处理后的pcd文件重命名,然后放到指定的目录,尝试了好久最终做到了: 比方我想读取 "table_scene_lms400.pcd" 把它进行滤波处理,重命名为 &qu ...
- SAS 读取指定目录下文件列表宏
OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...
- SpringBoot学习:读取yml和properties文件的内容
一.在SpringBoot实现属性注入: 1).添加pom依赖jar包: <!-- 支持 @ConfigurationProperties 注解 --> <!-- https://m ...
- 检测web服务器指定位置大文件是否存在
在bugscan群里看到有人问有一个大文件,想探测其是否存在.如果使用curl的话,会将整个文件下载到节点,对于扫描没有任何用处,反而浪费了扫描时间. 于是我想到的解决办法是不使用curl,直接用底层 ...
- 可以在一个html的文件当中读取另一个html文件的内容
1.IFrame引入,看看下面的代码 <IFRAME NAME="content_frame" width=100% height=30 marginwidth=0 marg ...
- C#Unit单元测试之读取Web.config文件
长期一来,我们所完成的项目都没有写单元测试,今天我一时兴起,决定给自己写的代码写单元测试,简单的测试代码分分钟完成了,一运行测试,就懵逼了.没能达到我的预期效果,而是出现图1所示错误. 图1:单元测试 ...
随机推荐
- 5.7 C++函数调用操作符重载
参考:http://www.weixueyuan.net/view/6385.html 总结: 需要以类成员函数的形式对函数调用操作符“()”进行重载. 只有常成员函数才能处理常对象,故我们依然在类中 ...
- DevExpress WinForms使用教程:SVG图库和Image Picker
[DevExpress WinForms v18.2下载] 每个新版本都在几个新控件中引入了矢量图标支持. 对于v18.2,这是列表: BackstageViewControl及其项目 RecentI ...
- elk之elasticsearch安装
环境: centos7 jdk8 参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.htmlhttp:// ...
- Ionic2开发环境搭建、项目创建调试与Android应用的打包、优化
Ionic2开发环境搭建.项目创建调试与Android应用的打包.优化. windows下ionic2开发环境配置步骤如下: 下载node.js环境,稳定版本:v6.9.5 下载android stu ...
- 1.Python爬虫入门一之综述
要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy Python爬虫 ...
- Effective Java Chapter4 Classes and Interface
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
- JavaWeb:c3p0配置问题-----java.sql.SQLException: Connections could not be acquired from the underlying database!
错误原因 c3p0的配置错误 错误显示 -classpath "D:\Program\Software\IntelliJIDEA\IntelliJ IDEA 2018.2.5\lib\ide ...
- kbmMW SmartService控制返回类型
- JAVA项目中的常用的异常处理
1. java.lang.nullpointerexception这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对象 ...
- chrome插件 - Manifest文件中的 background
在Manifest中指定background域可以使扩展常驻后台. background可以包含三种属性,分别是scripts.page和persistent. 如果指定了scripts属性,则Chr ...