zoukankan      html  css  js  c++  java
  • 转载:轻量级Config文件AppSettings节点编辑帮助类

    using System.Configuration;
    using System.Windows.Forms;
    
    namespace Allyn.Common
    {
        public class XmlHeper
        {
            ///<summary> 
            ///返回Config文件中appSettings配置节的value项  
            ///</summary> 
            ///<param name="strKey">节点Key</param> 
            ///<returns></returns> 
            public static string GetAppConfig(string strKey)
            {
                string file = Application.ExecutablePath;
                Configuration config = ConfigurationManager.OpenExeConfiguration(file);
    
                foreach (string key in config.AppSettings.Settings.AllKeys)
                {
                    if (key == strKey)
                    {
                        return config.AppSettings.Settings[strKey].Value.ToString();
                    }
                }
                return string.Empty;
            }
    
            ///<summary>  
            ///在Config文件中appSettings配置节增加一对键值对  
            ///</summary>  
            ///<param name="newKey">节点名称</param>  
            ///<param name="newValue">信值</param>  
            public static void UpdateAppConfig(string newKey, string newValue)
            {
                string file = System.Windows.Forms.Application.ExecutablePath;
                Configuration config = ConfigurationManager.OpenExeConfiguration(file);
    
                bool exist = false;
    
                foreach (string key in config.AppSettings.Settings.AllKeys)
                {
                    if (key == newKey) {  exist = true; }
                }
    
                if (exist)  { config.AppSettings.Settings.Remove(newKey); }
    
                config.AppSettings.Settings.Add(newKey, newValue);
                config.Save(ConfigurationSaveMode.Modified);
    
                ConfigurationManager.RefreshSection("appSettings");
            }
        }
    }

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

  • 相关阅读:
    [转]C#里 泛型Where和 new()的使用
    测试
    C#中的static、readonly与const的比较
    将字符串格式化变为两位
    在VS中对字段进行包装
    安装mysql的心得
    关于mysql数据库的乱码问题
    timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
    解决向数据库mysql插入double数据小数点不显示问题
    JDBOOK
  • 原文地址:https://www.cnblogs.com/uftwkb24/p/10191935.html
Copyright © 2011-2022 走看看