asp.net代码中修改web.config节点的具体方法
更新时间:2013年06月11日 23:21:52 作者:
在有些情况下,要在代码中读取一种全局变量,把这种全局变量放在web.config是一种常见的手段。
但是这个变量不会一个固定的值,会根据实际情况而发生变化,比如在需要读取一个配置文件的路径,而这个路径是站点发布的实际硬盘路径,如果直接是编译时状态,没有问题。但是如果站点iis更换路径,就需要修改这个web.config中的参数。如果能将这个编译时状态修改为运行时状态,那将更为合理和方便。这就需要存在一种在代码中能够动态修改web.config的方案。
代码
/// <summary>
/// 写入web.config
/// </summary>
/// <param name="item">appSettings等</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
public void WriteConfig(string item, string key, string value)
{
if (item == "")
{
item = "appSettings";
}
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);
if (appSection.Settings[key] == null)
{
appSection.Settings.Add(key, value);
config.Save();
}
else
{
appSection.Settings.Remove(key);
appSection.Settings.Add(key, value);
config.Save();
}
}
代码
复制代码 代码如下:
/// <summary>
/// 写入web.config
/// </summary>
/// <param name="item">appSettings等</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
public void WriteConfig(string item, string key, string value)
{
if (item == "")
{
item = "appSettings";
}
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);
if (appSection.Settings[key] == null)
{
appSection.Settings.Add(key, value);
config.Save();
}
else
{
appSection.Settings.Remove(key);
appSection.Settings.Add(key, value);
config.Save();
}
}
您可能感兴趣的文章:
- 详解ASP.NET配置文件Web.config
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- asp.net web.config加密解密方法
- ASP.NET(C#)应用程序配置文件app.config/web.config的增、删、改操作
- asp.net Web.config 详细配置说明
- asp.net 禁用viewstate在web.config里
- asp.net access web.config denied
- 认识ASP.NET配置文件Web.config
- ASP.NET配置文件Web.config用法详解
- ASP.NET中Web.config文件的层次关系详细介绍
- ASP.NET webUploader上传大视频文件相关web.config配置
相关文章
asp.net(c#)动态修改webservice的地址和端口(动态修改配置文件)
这个问题其实并没有我想像的那个复杂,我们都知道怎么直接修改吧,那就是修改WebConfig文件的配置节2012-12-12
使用本机IIS Express开发Asp.Net Core应用图文教程
IIS Express是一个Mini版的IIS,能够支持所有的Web开发任务,本篇经验将和大家介绍使用自定义主机名来访问运行在IIS Express上的站点程序的方法,希望对大家的工作和学习有所帮助2023-06-06
ASP.NET Core使用JWT自定义角色并实现策略授权需要的接口
这篇文章介绍了ASP.NET Core使用JWT自定义角色并实现策略授权需要的接口,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-01-01
关于服务器或虚拟主机不支持 AjaxPro 的问题终极解决方法
asp.net的网站,访问时提示不支持 AjaxPro,那就因为误删的映射导致,可以通过下面的方法解决2012-03-03


最新评论