需求:
什么时候会用到动态改变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://*.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();
}
} }
}