轻量级Config文件AppSettings节点编辑帮助类

 1 using System.Configuration;
 2 using System.Windows.Forms;
 3 
 4 namespace Allyn.Common
 5 {
 6     public class XmlHeper
 7     {
 8         ///<summary> 
 9         ///返回Config文件中appSettings配置节的value项  
10         ///</summary> 
11         ///<param name="strKey">节点Key</param> 
12         ///<returns></returns> 
13         public static string GetAppConfig(string strKey)
14         {
15             string file = Application.ExecutablePath;
16             Configuration config = ConfigurationManager.OpenExeConfiguration(file);
17 
18             foreach (string key in config.AppSettings.Settings.AllKeys)
19             {
20                 if (key == strKey)
21                 {
22                     return config.AppSettings.Settings[strKey].Value.ToString();
23                 }
24             }
25             return string.Empty;
26         }
27 
28         ///<summary>  
29         ///在Config文件中appSettings配置节增加一对键值对  
30         ///</summary>  
31         ///<param name="newKey">节点名称</param>  
32         ///<param name="newValue">信值</param>  
33         public static void UpdateAppConfig(string newKey, string newValue)
34         {
35             string file = System.Windows.Forms.Application.ExecutablePath;
36             Configuration config = ConfigurationManager.OpenExeConfiguration(file);
37 
38             bool exist = false;
39 
40             foreach (string key in config.AppSettings.Settings.AllKeys)
41             {
42                 if (key == newKey) {  exist = true; }
43             }
44 
45             if (exist)  { config.AppSettings.Settings.Remove(newKey); }
46 
47             config.AppSettings.Settings.Add(newKey, newValue);
48             config.Save(ConfigurationSaveMode.Modified);
49 
50             ConfigurationManager.RefreshSection("appSettings");
51         }
52     }
53 }

 

转载于:https://www.cnblogs.com/allyn/p/10178067.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值