/// <summary>
/// 添加和更新配置文件web.config的appSettings,缺点是会删除注释
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddUpdateAppSettings(string key, string value)
{
try
{
var configFile = WebConfigurationManager.OpenWebConfiguration("~");
var settings = configFile.AppSettings.Settings;
try
{
if (settings[key] == null)
{
settings.Add(key, value);
}
else
{
settings[key].Value = value;
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}
catch (Exception ex)
{
if (settings.Count != )
{
NLog.Error("配置文件写入失败,需要写入文件的配置信息:===============================");
foreach (string tkey in settings.AllKeys)
{
string tvalue = settings[tkey].Value;
NLog.Error(string.Format("Key: {0} Value: {1}", tkey, tvalue));
}
}
throw;
}
}
catch (Exception ex)
{
NLog.Error("具体失败内容:" + ex);
}
} /// <summary>
/// 添加和更新web.config的appSettings,且不删除注释
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void UpdateAppSetting(string key, string value)
{
try
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
SaveUsingXDocument(key, value, config.AppSettings.ElementInformation.Source);
}
catch (Exception ex)
{
NLog.Error("更新配置失败" + ex);
}
}
private static void SaveUsingXDocument(string key, string value, string fileName)
{
XDocument document = XDocument.Load(fileName);
if (document.Root == null)
{
return;
}
var appSettings = document.Root.Element("appSettings");
if (appSettings == null) return;
XElement appSetting = appSettings.Elements("add").FirstOrDefault(x => x.Attribute("key").Value == key);
if (appSetting != null)
{
appSetting.Attribute("value").Value = value;
}
else
{
XElement ele = new XElement("add", new XAttribute("key", key), new XAttribute("value", value));
appSettings.Add(ele);
}
document.Save(fileName);
}

c# 更新web.config的更多相关文章

  1. 微软ASP.NET网站部署指南(3):使用Web.Config文件的Transformations

    1. 综述 大多数程序里都会在Web.config里设置參数,而且在部署的时候须要更改. 每次都手工更改这些配置非常乏味,也easy出错. 该章节将会告诉你假设通过自己主动化更新Web.config文 ...

  2. 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)

    Web.config的读取 对于Web.config的读取大家都很属性了.平时我们用得比较多的就是appSettings节点下配置.如: 我们对应的代码是: = ConfigurationManage ...

  3. Linux下用ftp更新web内容!

    使用ftp更新web!让网页更新一次OK! 配置如下: 1.在Linux下安装ftp服务器! yum -y install vsftpd #ftp由vsftpd提供! 2.配置主配置文件/etc/vs ...

  4. ASP.NET Web.config

    分析: .NET Web 应用程序的配置信息(如最常用的设置ASP.Net Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过VB.NET新 建 一个Web应用程序后,默认 ...

  5. 【Hello CC.NET】自动化发布时 Web.config 文件维护

    在 <[Hello CC.NET]CC.NET 实现自动化集成> 的 HellowWorld 中经实现: 1.获取源码 2.编译项目 3.集成测试 4.Ftp发布项目 5.创建安装包 6. ...

  6. 两种读写配置文件的方案(app.config与web.config通用)

    第一种方法:采用MS现有的ConfigurationManager来进行读写 using System.Configuration; namespace Zwj.TEMS.Common { publi ...

  7. ASP.Net Web.config 中引用外部config文件

    1. 前提准备: Web.config file: <?xml version="1.0" encoding="utf-8"?><config ...

  8. membership 在web.config中配置信息

    <?xml version="1.0" encoding="utf-8"?><configuration> <configSect ...

  9. web.config的数据库连接字符串进行加密

    连接参考:http://wenku.baidu.com/link?url=nwGug8wxz143A4pvBE_kN6vMU7aF3ojwCKJOyN-TQleZ07iAYrjx_FnFVDOtZAF ...

随机推荐

  1. javaweb利用filter拦截未授权请求

    项目上有个小需求,要限制访问者的IP,屏蔽未授权的请求.该场景使用过滤器来做再合适不过了. SecurityFilter.java: public class SecurityFilter imple ...

  2. js操作表格、table、

    js添加一行.删除一行 let str="<tr>" +"<td>"+a[1]+"</td>" +&qu ...

  3. Why you need to understand garbage collection

    Why you need to understand garbage collection I’ve been interviewing lots of C# developers recently, ...

  4. dubbo中的Filter链原理及应用

    转载:https://www.jianshu.com/p/f390bb88574d filter在dubbo中的应用非常广泛,它可以对服务端.消费端的调用过程进行拦截,从而对dubbo进行功能上的扩展 ...

  5. vs2015 项目调试出现未能加载文件或程序集“Antlr3.Runtime”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    今天在调试项目不知道怎么了,突然就报未能加载文件或程序集“Antlr3.Runtime”或它的某一个依赖项.找到的程序集清单定义与程序集引用不匹配. (异常来自 HRESULT:0x80131040) ...

  6. CentOS 使用官方源yum安装最新nginx版本

    CentOS 使用官方源yum安装最新nginx版本 1.创建nginx.repo # vi /etc/yum.repos.d/nginx.repo 2.添加内容#如果是CentOS6,文件内容如下: ...

  7. UPDATE REPLACE 替换"\\"

    UPDATE Working_InterfaceToManager SET StudyPhotoInfoList = REPLACE(StudyPhotoInfoList,"\\\\&quo ...

  8. Embed MP4 in HTML using flash-player(html5 video player)

    https://stackoverflow.com/questions/1000851/embed-mp4-in-html-using-flash-player ******************* ...

  9. docker 安装jenkins 发布 asp.net core 2.0

    安装Docker 其实安装Docker的过程,大家可以到Docker官网找到自己相对应的安装文档进行安装,Docker区分CE和EE的两个版本,具体这两个版本有什么区别,大家自行查阅相关资料,这里不再 ...

  10. SDN实验---Ryu的应用开发(四)北向接口RESTAPI

    一:推文 软件定义网络基础---REST API概述 软件定义网络基础---REST API的设计规范 二:掌握Ryu基本RESTAPI使用方法 (一)Ryu的RESTAPI (二) REST应用样例 ...