zoukankan      html  css  js  c++  java
  • 轻量级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 }
  • 相关阅读:
    B1001 害死人不偿命的(3n+1)猜想 (15 分)
    A1050 String Subtraction (20 分)
    A1041 Be Unique (20 分)
    B1047 编程团体赛 (20 分)
    B1043 输出PATest (20 分)
    B1042 字符统计 (20 分)
    B1038 统计同成绩学生 (20 分)
    VB计算符号
    vs2008写代码的时候不能输入中文,sogou和google输入法都没有用
    如何彻底关闭Windows7自动更新
  • 原文地址:https://www.cnblogs.com/allyn/p/10178067.html
Copyright © 2011-2022 走看看