/// <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. DT包 -- R语言中自定义表格数据

    DT 包提供了 JavaScript 库 DataTables 的一个R接口,它使得R对象(矩阵或数据框)可以在HTML页面上显示为表格. 该包的DataTables函数生成的表格提供了数据的筛选.分 ...

  2. bind named.conf 的理解

    [root@46 /]#yum -y install bind bind-chroot bind-libs bind-utils caching-nameserver目录说明/var/named/ch ...

  3. python gaussian,gaussian2

    import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.axisartist as axisartist from ...

  4. Android Sensor 架构深入剖析【转】

    本文转载自: 1.Android sensor架构 Android4.0系统内置对传感器的支持达13种,它们分别是:加速度传感器 (accelerometer).磁力传感器(magnetic fiel ...

  5. CRT 上传下载文件

    yum install -y lrzsz 上传 rz -be 下载 sz 文件名

  6. how to transfer your linux to new work environment

    tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --ex ...

  7. Java13新特性 -- ZGC:取消使用未使用的内存

    在JDK 11中,Java引入了ZGC,这是一款可伸缩的低延迟垃圾收集器,但是当时只是实验性的.号称不管你开了多大的堆内存,它都能保证在 10 毫秒内释放 JVM ,不让它停顿在那.但是,当时的设计是 ...

  8. 查找算法(7)--Hash search--哈希查找

    1.哈希查找 (1)什么是哈希表(Hash) 我们使用一个下标范围比较大的数组来存储元素.可以设计一个函数(哈希函数, 也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标)相对应,于是用 ...

  9. Centos7安装完成后设定基本的网络配置

    Centos7设定网络 新安装的centos7,网络默认是不启动的,需要人为的手工修改配置文件,在这里把这个过程简要的记录一下. 设定ip地址与mac地址自定义 [root@web ~]# cd /e ...

  10. google cloud storage products

    https://cloud.google.com/products/storage/ BigTable Cloud Bigtable 是 Google 面向大数据领域的 NoSQL 数据库服务.它也是 ...