• 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>
  • 相关阅读:
    子类继承方法的重写
    操作系统的用户模式和内核模式
    Java中的CAS
    FaceBook SDK登录功能实现(Eclipse)
    eclipse集成ijkplayer项目
    android handler传递数据
    android发送短信
    hadoop中的job.setOutputKeyClass与job.setMapOutputKeyClass
    mysql对事务的支持
    使用jd-gui+javassist修改已编译好的class文件
  • 原文地址:https://www.cnblogs.com/xifengyeluo/p/8276118.html
Copyright © 2020-2023  润新知