不保留

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,保留注释与不保留注释的更多相关文章

  1. C# 读写App.config

    Jul142013 [C#] 读写App.config配置文件的方法 作者:xieyc   发布:2013-07-14 17:29   字符数:3433   分类:编程   阅读: 39,139 次 ...

  2. C#读写app.config中的数据

    C#读写app.config中的数据 读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Con ...

  3. C#中动态读写App.config配置文件

    转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...

  4. C# 读写App.config配置文件

    一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...

  5. 【C#】【WPF】如何读写app.config文件

    WPF生成的项目中会有.exe.config.一般是系统默认配置的 格式是xml格式,C#的项目可以直接读写这些文件.方法代码如下. public static string GetConnectio ...

  6. C# 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  7. 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  8. 关于读写APP.config文件能读却写不了的问题

    今天要求用winform写一个窗口用来读写一个App.config,要对  <appSettings>里面的add key和value进行添加和修改.要实现的效果图如下: -------- ...

  9. WinForm读写App.config配置文件

    一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...

随机推荐

  1. 1年内4次架构调整,谈Nice的服务端架构变迁之路

    Nice 本身是一款照片分享社区类型的应用,在分享照片和生活态度的同时可以在照片上贴上如品牌.地点.兴趣等tag. Nice从2013.10月份上线App Store到目前每天2亿PV,服务端架构经过 ...

  2. vue - static(.gitkeep)

    描述:git上传忽略的文件,与.gitnoignore一样.

  3. 在retrofit+Rxjava中如何取得状态码非200(出现错误)时的response里的body

    一个典型的retrofit+Rxjava的网络请求如下 Subscription subscription = videoChartService.login(newBody) .observeOn( ...

  4. 解决Windows下,运行python工程下的模块找不的到问题

    1.首先在Windows下配置环境变量 找到python安装的目录,我是装在C:\Python27,将改路径添加到环境变量中 添加成功了,你可以在任意行下面执行 python 程序 当你在执行工程的时 ...

  5. Vue 组件与复用

    (1)全局注册 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="U ...

  6. 读-《c++设计新思维-泛型编程与设计模式之应用》经典记录(英文书名:《modern c++ design》)

    1.以设计为目标的程序库都必须帮助使用者完毕静止的设计.以实现使用者自己的constraints,而不是实现预先定义好的constraints. 2.Anything that can be done ...

  7. 在进程中执行新代码 execl、execle、execlp、execv、execve和execvp函数

    摘要:本文主要讲述怎样在进程中执行新代码,以及exec系列函数的基本用法. 在进程中执行新代码 用函数fork创建子进程后,假设希望在当前子进程中运行新的程序,能够调用exec函数运行还有一个程序.当 ...

  8. ROC

    # -*- coding: utf-8 -*- # __author__ = "JieYao" from biocluster.agent import Agent from bi ...

  9. java 状态对象

    package cn.itcast.hibernate.sh.state; import org.hibernate.Session; import org.hibernate.Transaction ...

  10. linux磁盘满时,如何定位并删除文件

    原文链接: http://www.cnblogs.com/yinxiangpei/articles/4211743.html @1.一般情况   一般情况下先df看一下,然后cd到要满的盘,执行: d ...