需求:

什么时候会用到动态改变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. 3.6 C++继承机制下的构造函数

    参考:http://www.weixueyuan.net/view/6363.html 总结: 在codingbook类中新增了一个language成员变量,为此必须重新设计新的构造函数.在本例中bo ...

  2. shiro简单学习的简单总结

    权限和我有很大渊源. 培训时候的最后一个项目是OA,权限那块却不知如何入手,最后以不是我写的那个模块应付面试. 最开始的是使用session装载用户登录信息,使用简单权限拦截器做到权限控制,利用资源文 ...

  3. L295 how to turn down a job but keep a good relationship with the hiring manager

    Let’s say you’re on the hunt for a new job. Three interviews in, you realize it’s not the place for ...

  4. 点击li ,父辈出现; 子级,子辈不出现. prevUntil ---> 前面多个, 截止到 截止元素 , prev([expr]) --> 前面一个.

    要求: 1. 点击第一级   [1知识点] 的时候,  [1知识点] 前有 圆圈. 点击 第二级 [1-1知识点, 1-2知识点, 1-3知识点] 时 , [1知识点]出现 圆圈. 2. 点击 第一级 ...

  5. java学习笔记6(面向对象1:概念,private)

    1.思想: 面向过程的思想:遇到问题时想,我该如何做,然后分步骤实现: 面向对象的思想:遇到问题时想,我该派谁去做这件事,至于他怎么做,与我无关,我只要最后的结果. 实际举例:我们要组装一台电脑: 面 ...

  6. Fatal error: Call to undefined function mb_strlen()

    php配置的时候出现:Fatal error:  Call to undefined function mb_strlen() 表示php不能加载mbstring模块,在php 的配置文件php.in ...

  7. jQuery.Deferred exception: $.get is not a function TypeError: $.get is not a function

    /********************************************************************** * jQuery.Deferred exception: ...

  8. N!的近似值_斯特林公式

    公式: N! ~=  sqrt(2 * PI * n) * ((n / e) ^n) 题目类型不慌都.

  9. opencv的DMatch

    1.DMatch是描述图像匹配信息的类 /** @brief Class for matching keypoint descriptors query descriptor index, train ...

  10. Redis实战之Redis命令

    阅读目录 1. 字符串命令 2. 列表命令 3. 集合命令 4. 散列命令 5. 有序集合命令 6. 发布与订阅命令 7. 小试牛刀 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结 ...