需求:

什么时候会用到动态改变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"];
 
(2)添加
        Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
        AppSettingsSection app = config.AppSettings;
        app.Settings.Add("p", "p:\\");
        config.Save(ConfigurationSaveMode.Modified);
 
(3)修改
          Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
            AppSettingsSection app = config.AppSettings;
            app.Settings["p"].Value = @"g:\";
            config.Save(ConfigurationSaveMode.Modified);
 
(4)删除
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
        AppSettingsSection app = config.AppSettings;
        app.Settings.Remove("p");
        config.Save(ConfigurationSaveMode.Modified);
 
 
 
注意:
(1)asp.net用户需要有读取、修改、写入的权限。
(2)WebConfigManager是web.config所在的文件夹名。

出处: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文件的内容的更多相关文章

  1. WinForm读取指定的config文件的内容

    config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...

  2. php读取指定结束指针文件内容

    fopen操作时文件读取开始指针位于文件开始部分, fseek 以指定文件大小以及开始指针位置确定结束指针位置 具体案例: <?php//打开文件流,fopen不会把文件整个加载到内存$f = ...

  3. 【ASP.NET 进阶】定时执行任务实现 (定时读取和修改txt文件数字内容,无刷新显示结果)

    现在有很多网站或系统需要在服务端定时做某件事情,如每天早上8点半清理数据库中的无效数据等等,Demo 具体实现步骤如下: 0.先看解决方案截图 1.创建ASP.NET项目TimedTask,然后新建一 ...

  4. PCL点云库中怎样读取指定的PCD文件,又一次命名,处理后保存到指定目录

    我一直想把处理后的pcd文件重命名,然后放到指定的目录,尝试了好久最终做到了: 比方我想读取  "table_scene_lms400.pcd" 把它进行滤波处理,重命名为 &qu ...

  5. SAS 读取指定目录下文件列表宏

    OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...

  6. SpringBoot学习:读取yml和properties文件的内容

    一.在SpringBoot实现属性注入: 1).添加pom依赖jar包: <!-- 支持 @ConfigurationProperties 注解 --> <!-- https://m ...

  7. 检测web服务器指定位置大文件是否存在

    在bugscan群里看到有人问有一个大文件,想探测其是否存在.如果使用curl的话,会将整个文件下载到节点,对于扫描没有任何用处,反而浪费了扫描时间. 于是我想到的解决办法是不使用curl,直接用底层 ...

  8. 可以在一个html的文件当中读取另一个html文件的内容

    1.IFrame引入,看看下面的代码 <IFRAME NAME="content_frame" width=100% height=30 marginwidth=0 marg ...

  9. C#Unit单元测试之读取Web.config文件

    长期一来,我们所完成的项目都没有写单元测试,今天我一时兴起,决定给自己写的代码写单元测试,简单的测试代码分分钟完成了,一运行测试,就懵逼了.没能达到我的预期效果,而是出现图1所示错误. 图1:单元测试 ...

随机推荐

  1. 《图解HTTP》读书笔记(转)

    reference:https://www.cnblogs.com/edisonchou/p/6013450.html   目前国内讲解HTTP协议的书是在太少了,记忆中有两本被誉为经典的书<H ...

  2. easyui再学习的一部分代码

    <%-- Created by IntelliJ IDEA. User: zhen Date: // Time: : To change this template use File | Set ...

  3. powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model.

    powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model ...

  4. 使用kafka和zookeeper 构建分布式编译环境

    1:在每台机器上安装jdk, 脚本代码如下: 每一个机器上下载jdk,zookeeper,kafka 链接:https://www.oracle.com/technetwork/java/javase ...

  5. shell脚本实例-内存磁盘使用警告

    1,磁盘使用警告并发送邮件 #!usr/bin/bash #df -Th|grep '/$' 这个是获取内存使用的那一条记录 #后面两句是获取内存的使用率 disk=`df -Th|grep '/$' ...

  6. mac下python2.7升级到3.6

    1. 前言 Mac系统自带python2.7,本文目的是将自带的python升级到3.6版本. 网上有本多的做法是让python2.7和python3.X两个版本共存,博主并不知道,是两版本共存好,还 ...

  7. C#清理所有正在使用的资源

    namespace QQFrm{    partial class Form1    {        /// <summary>        /// 必需的设计器变量.        ...

  8. debian镜像下载地址

     http://cdimage.debian.org/debian-cd/9.8.0-live/amd64/iso-hybrid/ 

  9. Java学习笔记29(IO字符流,转换流)

    字符流:只能操作文本文件,与字节流的区别是,字节流是按照字节来读取文件,而字符流是按照字符来读取,因此字符流的局限性为文本文件 字符输出流:Write类,使用时通过子类   每一次写入都要刷新 pac ...

  10. Python基础5--字符串

    1 find().rfind().index().rindex().count() s = "this apple is red apple" s.find("apple ...