读写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 ...
 
随机推荐
- Android应用经典主界面框架之二:仿网易新闻client、CSDN client (Fragment ViewPager)
			
另外一种主界面风格则是以网易新闻.凤凰新闻以及新推出的新浪博客(阅读版)为代表.使用ViewPager+Fragment,即ViewPager里适配器里放的不是一般的View.而是Fragment.所 ...
 - java中的super限定
			
super的用法: (1)如果需要在子类中调用父类中被覆盖的实例方法,可以用super限定来调用父类中被覆盖的方法.当然,也可以调用从父类继承的实例变量. public void callOverri ...
 - OpenERP(odoo)开发实例之搜索检索过去3个月的数据
			
转自:http://www.chinamaker.net/ OpenERP(odoo)开发实例之搜索过滤:检索过去3个月的数据 解决这个问题的重点在于 relativedelta 的应用 示例代码如下 ...
 - 13-spring学习-class类型表达式
			
Class表达式 spring中对于class反射机制也有自己的处理. 1,class::使用T(类名)的形式可以取得一个指定泛型类型的Class对象. 范例: package com.Spring. ...
 - struts2自定义登录拦截器
			
版权声明:本文为博主原创文章,未经博主允许不得转载. (1)配置web.xml,让xml加载struts2框架 <?xml version="1.0" encoding=&q ...
 - SpringMVC学习小结
			
配置web.xml: <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-cl ...
 - 资源管理器也玩多标签:QT TabBar v1.5.0.0a3
			
http://zmingcx.com/explorer-also-play-more-tags-qt-tabbar-v1-5-0-0a3.html浏览器中的标签浏览功能非常受欢迎,安装QT TabBa ...
 - 使用DotNetZip压缩与解压缩
			
下载地址:http://dotnetzip.codeplex.com/ 解压后找到\\DotNetZipLib-DevKit-v1.9\zip-v1.9\Release下的Ionic.Zip.dll文 ...
 - jQuery Autocomplete  用户快速找到并从预设值列表中选择
			
jQuery Autocomplete 插件根据用户输入值进行搜索和过滤,让用户快速找到并从预设值列表中选择.通过给 Autocomplete 字段焦点或者在其中输入字符,插件开始搜索匹配的条目并显示 ...
 - stl之hash_multiset
			
hash_multiset的元素不会被自己主动排序