读写app.config AppSettings,保留注释与不保留注释
不保留
using System;
using System.Configuration; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ReadAllSettings();
ReadSetting("Setting1");
ReadSetting("NotValid");
AddUpdateAppSettings("NewSetting", "May 7, 2014");
AddUpdateAppSettings("Setting1", "May 8, 2014");
ReadAllSettings();
} static void ReadAllSettings()
{
try
{
var appSettings = ConfigurationManager.AppSettings; if (appSettings.Count == )
{
Console.WriteLine("AppSettings is empty.");
}
else
{
foreach (var key in appSettings.AllKeys)
{
Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
}
}
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error reading app settings");
}
} static void ReadSetting(string key)
{
try
{
var appSettings = ConfigurationManager.AppSettings;
string result = appSettings[key] ?? "Not Found";
Console.WriteLine(result);
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error reading app settings");
}
} static void AddUpdateAppSettings(string key, string value)
{
try
{
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
if (settings[key] == null)
{
settings.Add(key, value);
}
else
{
settings[key].Value = value;
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error writing app settings");
}
}
}
}
不删注释:
public static void SaveAppSettingsMethod2(string key, string value)
{
//验证key value
//To Do XmlDocument xml = new XmlDocument();
string configPath = Application.ExecutablePath + ".config";
xml.Load(configPath); XmlNodeList nodeList = xml.GetElementsByTagName("appSettings"); if (nodeList != null)
{
if (nodeList.Count >= )
{
XmlNode node = nodeList[];
foreach (XmlNode item in node)
{
if (item.NodeType == XmlNodeType.Comment)
{
continue;
} XmlAttribute xaKey = item.Attributes["key"];
XmlAttribute xaValue = item.Attributes["value"]; if (xaKey != null && xaValue != null && xaKey.Value.Equals(key))
{
xaValue.Value = value;
}
}
}
}
xml.Save(configPath);
}
读写app.config AppSettings,保留注释与不保留注释的更多相关文章
- C# 读写App.config
Jul142013 [C#] 读写App.config配置文件的方法 作者:xieyc 发布:2013-07-14 17:29 字符数:3433 分类:编程 阅读: 39,139 次 ...
- C#读写app.config中的数据
C#读写app.config中的数据 读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Con ...
- C#中动态读写App.config配置文件
转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...
- C# 读写App.config配置文件
一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...
- 【C#】【WPF】如何读写app.config文件
WPF生成的项目中会有.exe.config.一般是系统默认配置的 格式是xml格式,C#的项目可以直接读写这些文件.方法代码如下. public static string GetConnectio ...
- C# 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- 关于读写APP.config文件能读却写不了的问题
今天要求用winform写一个窗口用来读写一个App.config,要对 <appSettings>里面的add key和value进行添加和修改.要实现的效果图如下: -------- ...
- WinForm读写App.config配置文件
一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...
随机推荐
- 说说C#之父——安德斯·海尔斯伯格
安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi和.NET之父! 看到照片的那一刹那儿,我就觉得帅爆了,53岁的 ...
- CSS 盒状模型简介
框的构成以及相关 CSS 特性( property ) 结构 为了给文档树中的各个元素排版定位(布局),浏览器会根据渲染模型1为每个元素生成四个嵌套的矩形框, 分别称作 content box.pad ...
- TP框架模板中默认值输出
TP框架模板中默认值输出 我们可以给变量输出提供默认值,例如: {$user.nickname|default="这家伙很懒,什么也没留下"} 对系统变量依然可以支持默认值输出,例 ...
- CSS3文本溢出
text-overflow: text-overflow:clip | ellipsis; clip:剪切多余的文字. ellipsis:文本溢出时显示省略标记. 要实现文本溢出剪切显示省略标记,还需 ...
- js声明变量的三种方式及作用域
js声明变量的三种方式及作用域 CreateTime--2017年9月11日17:19:11 Author:Marydon 一.参考链接 本篇文章的创作灵感来源于博主-奔跑的铃铛关于js中cons ...
- Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.7
导入maven项目时,pom.xml文件报错如下: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2. ...
- python安装scrapy小问题总结
AttributeError: 'module' object has no attribute 'OP_NO_TLSv1_1'
- C#颜色 Color.FromArgb ColorTranslator 16进制
//方法1: //引用命名空间 using System.Drawing; 16进制颜色代码转Color类型:ColorTranslator.FromHtml(color); Color类型转16进制 ...
- SELinux 宽容模式(permissive) 强制模式(enforcing) 关闭(disabled)
SElinux共有3中状态.1.selinux的配置文件:/etc/selinux/config# This file controls the state of SELinux on the sys ...
- linux上定时备份mysql数据库
定时备份数据库 /usr/sbin/backupmysql timestamp=`date +"%Y-%m-%d-%H-%M-%S"` mysqldump -uroot -p'12 ...