VS2005(C#)里读取及改变App.config里appSettings节的值

  前几天做了一个C/S的项目,研究了一下App.config,写了一个读取及改变App.config里appSettings节的值的类,贴出来!


         
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  ServicesConfiguration
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ConfigurationDispose
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ConfigurationDispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetConfiguration#region GetConfiguration
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 取得appSettings里的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="key"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static string GetConfiguration(string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ConfigurationManager.AppSettings[key];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetConfigurationList#region GetConfigurationList
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 取得appSettings里的值列表
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="filePath">配置文件路径</param>
ExpandedSubBlockEnd.gif        
/// <returns>值列表</returns>

InBlock.gif        public static KeyValueConfigurationCollection GetConfigurationList(string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppSettingsSection appSection 
= null;                       //AppSection对象
InBlock.gif
            Configuration configuration = null;                         //Configuration对象     
InBlock.gif
            KeyValueConfigurationCollection k = null;                   //返回的键值对类型
InBlock.gif

InBlock.gif            configuration 
= ConfigurationManager.OpenExeConfiguration(filePath);
InBlock.gif
InBlock.gif            
//取得AppSettings节
InBlock.gif
            appSection = (AppSettingsSection)configuration.Sections["appSettings"];
InBlock.gif
InBlock.gif            
//取得AppSetting节的键值对
InBlock.gif
            k = appSection.Settings;
InBlock.gif
InBlock.gif            
return k;
InBlock.gif                    
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SetConfiguration#region SetConfiguration
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置appSetting的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="key"></param>
InBlock.gif        
/// <param name="value"></param>
ExpandedSubBlockEnd.gif        
/// <param name="filePath">App.config文件路径</param>

InBlock.gif        public static void SetConfiguration(string key, string value,string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Configuration configuration 
= null;                 //Configuration对象
InBlock.gif
            AppSettingsSection appSection = null;               //AppSection对象 
InBlock.gif
          
InBlock.gif            configuration 
= ConfigurationManager.OpenExeConfiguration(filePath);
InBlock.gif            
InBlock.gif            
//取得AppSetting节
InBlock.gif
            appSection = configuration.AppSettings;
InBlock.gif
InBlock.gif            
//赋值并保存
InBlock.gif
            appSection.Settings[key].Value = value;
InBlock.gif            configuration.Save();
InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SetConfiguration#region SetConfiguration
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置appSetting的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="key"></param>
ExpandedSubBlockEnd.gif        
/// <param name="value"></param>

InBlock.gif        public static void SetConfiguration(string key, string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppSettingsSection appSection 
= null;               //AppSection对象 
InBlock.gif

InBlock.gif            appSection 
= ConfigurationManager.GetSection("appSettings");
InBlock.gif
InBlock.gif            appSection.Settings[key].Value 
= value;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/hzuIT/articles/824326.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值