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 阅读(2014) 评论(0) 编辑 收藏 举报