C#编辑EXE使用的appSettings节点的Config文件

 

 

/// <summary>
        /// 保存配置文件的设定
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        public static void SaveAppConfig(string Key,string Value)
        {
            string strFilePath = System.Windows.Forms.Application.ExecutablePath;
            Configuration objConfig = ConfigurationManager.OpenExeConfiguration(strFilePath);
            bool bolExist = false;
            foreach (string Item in objConfig.AppSettings.Settings.AllKeys)
            {
                if (Item == Key)
                {
                    bolExist = true;
                    break;
                }
            }
            if (bolExist)
            {
                objConfig.AppSettings.Settings.Remove(Key);
            }
            objConfig.AppSettings.Settings.Add(Key,Value);
            objConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

  

posted @ 2018-12-13 00:45  DerekHan  阅读(449)  评论(0编辑  收藏  举报