当前位置: 首页 > news >正文

C# 操作配置文件 App.config

原文:http://www.cnblogs.com/253891489/articles/2008669.html

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Configuration;

namespace  Schwann.CommLibrary
{
    
public   class  ConfigHelper
    {
        
///   <summary>
        
///  根据键值获取配置文件
        
///   </summary>
        
///   <param name="key"> 键值 </param>
        
///   <returns></returns>
         public   static   string  GetConfig( string  key)
        {
            
string  val  =   string .Empty;
            
if  (ConfigurationManager.AppSettings.AllKeys.Contains(key))
                val 
=  ConfigurationManager.AppSettings[key];
            
return  val;
        }

        
///   <summary>
        
///  获取所有配置文件
        
///   </summary>
        
///   <returns></returns>
         public   static  Dictionary < string string >  GetConfig()
        {
            Dictionary
< string string >  dict  =   new  Dictionary < string string > ();
            
foreach  ( string  key  in  ConfigurationManager.AppSettings.AllKeys)
                dict.Add(key, ConfigurationManager.AppSettings[key]);
            
return  dict;
        }

        
///   <summary>
        
///  根据键值获取配置文件
        
///   </summary>
        
///   <param name="key"> 键值 </param>
        
///   <param name="defaultValue"> 默认值 </param>
        
///   <returns></returns>
         public   static   string  GetConfig( string  key,  string  defaultValue)
        {
            
string  val  =  defaultValue;
            
if  (ConfigurationManager.AppSettings.AllKeys.Contains(key))
                val 
=  ConfigurationManager.AppSettings[key];
            
if  (val  ==   null )
                val 
=  defaultValue;
            
return  val;
        }

        
///   <summary>
        
///  写配置文件,如果节点不存在则自动创建
        
///   </summary>
        
///   <param name="key"> 键值 </param>
        
///   <param name="value"> </param>
        
///   <returns></returns>
         public   static   bool  SetConfig( string  key,  string  value)
        {
            
try
            {
                Configuration conf 
=  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                
if  ( ! conf.AppSettings.Settings.AllKeys.Contains(key))
                    conf.AppSettings.Settings.Add(key, value);
                
else
                    conf.AppSettings.Settings[key].Value 
=  value;
                conf.Save();
                
return   true ;
            }
            
catch  {  return   false ; }
        }

        
///   <summary>
        
///  写配置文件(用键值创建),如果节点不存在则自动创建
        
///   </summary>
        
///   <param name="dict"> 键值集合 </param>
        
///   <returns></returns>
         public   static   bool  SetConfig(Dictionary < string string >  dict)
        {
            
try
            {
                
if  (dict  ==   null   ||  dict.Count  ==   0 )
                    
return   false ;
                Configuration conf 
=  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                
foreach  ( string  key  in  dict.Keys)
                {
                    
if  ( ! conf.AppSettings.Settings.AllKeys.Contains(key))
                        conf.AppSettings.Settings.Add(key, dict[key]);
                    
else
                        conf.AppSettings.Settings[key].Value 
=  dict[key];
                }
                conf.Save();
                
return   true ;
            }
            
catch  {  return   false ; }
        }
    }
}

上一篇学习了配置文件读取的处理方式,但是没有对经常用到的 <appSettings>进行学习,其实这些参数的读取要简单得多:
假设有如下配置参数

  <appSettings>
    <add key="address" value="China"/>
  </appSettings>

读取:

string address=System.Configuration.ConfigurationManager.AppSettings["address"].ToString();

事实就是这么简单

相关文章:

  • iPhone入华情变 谁动了中移动的苹果?
  • C# 托盘程序
  • C# 托盘程序 实例 双击显示窗体,最小化到托盘
  • Myeclipse6.0的破解方法
  • 免费的网页原型制作工具
  • c#获取机器码
  • 稻 香
  • Access获取年月日
  • C# EXCEL导入 混合列读取文字为空的解决办法
  • 推荐系统管理员一读的书!
  • sql 2005 用户 sa 登录失败,该用户与可信SQL Server连接无关联 错误18452
  • linux的shell中,日期格式校验
  • ASP.NET MVC 中 Ajax.ActionLink 的使用
  • asp.net mvc2 数据验证方法
  • asp.net mvc 路由机制小记
  • 【Leetcode】101. 对称二叉树
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • JavaScript的使用你知道几种?(上)
  • maya建模与骨骼动画快速实现人工鱼
  • Spring核心 Bean的高级装配
  • VUE es6技巧写法(持续更新中~~~)
  • 简单数学运算程序(不定期更新)
  • 聚簇索引和非聚簇索引
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 云大使推广中的常见热门问题
  • 自制字幕遮挡器
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​​​​​​​​​​​​​​Γ函数
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (145)光线追踪距离场柔和阴影
  • (C语言)fread与fwrite详解
  • (LeetCode C++)盛最多水的容器
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (分类)KNN算法- 参数调优
  • (规划)24届春招和25届暑假实习路线准备规划
  • (每日持续更新)jdk api之FileFilter基础、应用、实战
  • (转)德国人的记事本
  • .bat文件调用java类的main方法
  • .gitignore文件---让git自动忽略指定文件
  • .net 7 上传文件踩坑
  • .NET Compact Framework 多线程环境下的UI异步刷新
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .NET/MSBuild 中的发布路径在哪里呢?如何在扩展编译的时候修改发布路径中的文件呢?
  • .net中的Queue和Stack
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • .pyc文件还原.py文件_Python什么情况下会生成pyc文件?
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • @拔赤:Web前端开发十日谈
  • []error LNK2001: unresolved external symbol _m
  • [BZOJ5250][九省联考2018]秘密袭击(DP)
  • [C#]使用PaddleInference图片旋转四种角度检测
  • [C++]unordered系列关联式容器