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:单元测试 ...
随机推荐
- SQL-1 选取表中某一属性最大值的所有信息 查找最晚入职员工的所有信息
题目描述 查找最晚入职员工的所有信息CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` date NOT NULL,`fi ...
- Win10安装mysql-8.0.11-winx64详细步骤
安装 mysql-8.0.11-winx64 https://blog.csdn.net/qq_20788055/article/details/80372577
- ajax返回数据定义为全局变量
var result; //定义全局变量 $(document).ready(function(){ $.ajax({ type:'PO ...
- do文件的编写(转)
以前在使用ModelSim进行仿真的时候,一直是使用其GUI进行操作的,但是这样很繁琐也很费时.故希望学习其自动化仿真do文件,下面是学习的一些总结. 一.编写基本的do文件 下面按照实际仿真的步骤来 ...
- DevExpress v18.1新版亮点——XAF篇(一)
用户界面套包DevExpress v18.1日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress eXpressApp Framework(XAF) v18.1 ...
- mysql 优化2
6. 合理使用EXISTS,NOT EXISTS子句.如下所示: 1.SELECT SUM(T1.C1) FROM T1 WHERE (SELECT COUNT(*)FROM T2 WHERE T2. ...
- Python 黏包及黏包解决方案
粘包现象 说粘包之前,我们先说两个内容,1.缓冲区.2.windows下cmd窗口调用系统指令 1 缓冲区(下面粘包现象的图里面还有关于缓冲区的解释) 每个 socket 被创建后,都会分配两个缓冲区 ...
- builtroot 添加git 下载方式
1.buildroot/Config.in 配置default git server eg:config xxxx_GIT_SITE string "git site" defau ...
- react-native 自定义 下拉刷新 / 上拉加载更多 组件
1.封装 Scroller 组件 /** * 下拉刷新/上拉加载更多 组件(Scroller) */ import React, {Component} from 'react'; import { ...
- redis的哨兵模式
我是在一台服务器上安装了三个redis 一主两从 想安装gcc gcc-c++ make tcl lrzsz yum -y install gcc gcc-c++ make tcl lrzsz ...