Winform修改配置文件节点保存到配置文件

主要使用:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//将当前应用程序的配置文件作为 System.Configuration.Configuration 对象打开。

config.Save();//刷新命名节,这样在下次检索它时将从磁盘重新读取它。

ConfigurationManager.RefreshSection("appSettings");//刷新命名节,这样在下次检索它时将从磁盘重新读取它。

 示例:

配置节点方法:

        /// <summary>
        /// 修改配置文件 节点值,保存并刷新
        /// </summary>
        public void SaveSettiongs(params string[] str)
        {
            if (XtraMessageBox.Show("是否保存修改?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//将当前应用程序的配置文件作为 System.Configuration.Configuration 对象打开。
                config.AppSettings.Settings["PathOne"].Value = str[0];
                config.AppSettings.Settings["PathTwo"].Value = str[1];
                config.AppSettings.Settings["PathThr"].Value = str[2];
                config.Save();//刷新命名节,这样在下次检索它时将从磁盘重新读取它。
                ConfigurationManager.RefreshSection("appSettings");//刷新命名节,这样在下次检索它时将从磁盘重新读取它。
                XtraMessageBox.Show("已保存修改");
            }
        }

保存按钮:

            SaveSettiongs(this.txtExcelPath.Text, this.txtStart.Text, this.txtEnd.Text);
       this.Close();

加载事件:

        private void FormPath_Load(object sender, EventArgs e)
        {
            this.txtPathOne.Text = ConfigurationManager.AppSettings["PathOne"].ToString();
            this.txtPathTwo.Text = ConfigurationManager.AppSettings["PathTwo"].ToString();
            this.txtPathThr.Text = ConfigurationManager.AppSettings["PathThr"].ToString();
        }

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <appSettings>
    <add key="PathOne" value="D:PathOne"/>
    <add key="PathTwo" value="D:PathTwo"/>
    <add key="PathThr" value="D:PathThr"/>
  </appSettings>
</configuration>
原文地址:https://www.cnblogs.com/xifengyeluo/p/8276118.html