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里的配置节:

<appSettings>
<add key="FilePath" value="g:\Test\WebConfigManager\Upload\" />
<add key="p" value="g:\" />
</appSettings>
(1)读
AppSettingsSection app = config.AppSettings;
app.Settings.Add("p", "p:\\");
config.Save(ConfigurationSaveMode.Modified);
AppSettingsSection app = config.AppSettings;
app.Settings["p"].Value = @"g:\";
config.Save(ConfigurationSaveMode.Modified);
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(); } } } }
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/9587917.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2018-09-04 19:10 jack_Meng 阅读(2025) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
2017-09-04 你真的了解分层架构吗?——写给被PetShop"毒害"的朋友们
2017-09-04 走进单元测试 ---- 系列文章
2017-09-04 C#.NET抽象类和接口的区别?
2017-09-04 读懂IL代码就这么简单 ---- IL系列文章
2017-09-04 C#中的依赖注入那些事儿
2017-09-04 关于SVN 目录结构