/// <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. spring的重试策略、发生异常会自动重新调用

    测试类以及测试代码.复制即可 package com.cms.util; import javax.swing.plaf.synth.SynthSpinnerUI; import org.spring ...

  2. 【Gamma】设计与计划

    目录 需求分析 已实现 功能 用户使用动机分析 当前阶段推广困难 当前阶段任务优先级 主要功能解析 社团活动场地申请 - 实现成本较高,正在调研社长的需求 完善入社流程的信息提示 通知功能 通知管理 ...

  3. 利用python实现汉字转拼音

    安装:pip install pypinyin import pypinyin # 不带声调的(style=pypinyin.NORMAL) def pinyin(word): s = '' for ...

  4. 如何查看android studio sdk路径配置

    file–>Other Settings–>Default Project Structure

  5. Nginx发布静态图片服务器

    vir-hosts.conf内容 server { listen ; server_name _; location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; ...

  6. windows 安装 Composer 报错

    错误信息如下: 解决方法: 在 extension = php_snmp.dll 前加上 ";" 然后重启 php 即可安装

  7. Java:JSON和Map相互转换

    Java:JSON与Map相互转换 需要jar包:jackson-core-2.6.0.jar,jackson-databind-2.6.0.jar,jackson-annotations-2.6.0 ...

  8. Python 多进程爬虫实例

    Python  多进程爬虫实例 import json import re import time from multiprocessing import Pool import requests f ...

  9. WebGL学习笔记(十):雾化

    雾化是指距离我们较远的物体看不清晰的情况,比如模拟大雾环境,或者模拟水下环境时会用到. 实现雾化的方式有很多种,我们这里使用的是线性雾化的方式: 线性雾化 线性雾化比较简单,我们算出每个像素点到摄像机 ...

  10. C++ Set运用实例

    C++ Set运用实例 #include <iostream> #include <set> #include <algorithm> #include <i ...