c# 更新web.config
/// <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的更多相关文章
- 微软ASP.NET网站部署指南(3):使用Web.Config文件的Transformations
1. 综述 大多数程序里都会在Web.config里设置參数,而且在部署的时候须要更改. 每次都手工更改这些配置非常乏味,也easy出错. 该章节将会告诉你假设通过自己主动化更新Web.config文 ...
- 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)
Web.config的读取 对于Web.config的读取大家都很属性了.平时我们用得比较多的就是appSettings节点下配置.如: 我们对应的代码是: = ConfigurationManage ...
- Linux下用ftp更新web内容!
使用ftp更新web!让网页更新一次OK! 配置如下: 1.在Linux下安装ftp服务器! yum -y install vsftpd #ftp由vsftpd提供! 2.配置主配置文件/etc/vs ...
- ASP.NET Web.config
分析: .NET Web 应用程序的配置信息(如最常用的设置ASP.Net Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过VB.NET新 建 一个Web应用程序后,默认 ...
- 【Hello CC.NET】自动化发布时 Web.config 文件维护
在 <[Hello CC.NET]CC.NET 实现自动化集成> 的 HellowWorld 中经实现: 1.获取源码 2.编译项目 3.集成测试 4.Ftp发布项目 5.创建安装包 6. ...
- 两种读写配置文件的方案(app.config与web.config通用)
第一种方法:采用MS现有的ConfigurationManager来进行读写 using System.Configuration; namespace Zwj.TEMS.Common { publi ...
- ASP.Net Web.config 中引用外部config文件
1. 前提准备: Web.config file: <?xml version="1.0" encoding="utf-8"?><config ...
- membership 在web.config中配置信息
<?xml version="1.0" encoding="utf-8"?><configuration> <configSect ...
- web.config的数据库连接字符串进行加密
连接参考:http://wenku.baidu.com/link?url=nwGug8wxz143A4pvBE_kN6vMU7aF3ojwCKJOyN-TQleZ07iAYrjx_FnFVDOtZAF ...
随机推荐
- CCF认证 201903-4消息传递接口
题目 代码 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stri ...
- 【Dubbo】带着问题看源码:什么是SPI机制?Dubbo是如何实现的?
什么是SPI? 在Java中,SPI全称为 Service Provider Interface,是一种典型的面向接口编程机制.定义通用接口,然后具体实现可以动态替换,和 IoC 有异曲同工之妙. ...
- C# Microsoft.Office.Interop.Owc11 导出excel文件
C# Microsoft.Office.Interop.Owc11 导出excel文件 1.新建项SupremeWindowsForms窗体应用项目(项目平台设置称X86) 注意:因为大多数第三方写的 ...
- webpack的带表达式require和require.context()方法
带表达式的 require 语句如果 require参数含有表达式(expressions),会创建一个上下文(context),因为在编译时(compile time)并不清楚具体是哪一个模块被导入 ...
- eclispe: 修改所有文件默认编码为UTF-8
1.修改 workspace text file encoding 依次点击windows -> Preferences -> general -> Workspace,修改如图的编 ...
- dubbo连接过程
场景1 启动时 dubbo服务提供端在Zookeeper上注册的节点目录:假设接口名称是:com.bob.dubbo.service.CityDubboService dubbo服务提供端连接到注册中 ...
- Xamarin.FormsShell基础教程(7)Shell项目关于页面的介绍
Xamarin.FormsShell基础教程(7)Shell项目关于页面的介绍 轻拍标签栏中的About标签,进入关于页面,如图1.8和图1.9所示.它是对应用程序介绍的页面. 该页面源自Views文 ...
- 基于realsense的深度数据分析工具
- 2019 GDD breaking world‘s record of π
Day 2 1.breaking pi‘s world record with google cloud [concept] memory wall: Originally theorized in ...
- CentOS7使用yum安装redis
#下载fedora的epel仓库yum install epel-release #安装redis数据库yum install redis # 启动Redisservice redis start # ...