zoukankan      html  css  js  c++  java
  • 关于读写APP.config文件能读却写不了的问题

      今天要求用winform写一个窗口用来读写一个App.config,要对  <appSettings>里面的add key和value进行添加和修改。要实现的效果图如下:

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    读的功能很容易实现:

    txtIp.Text = ConfigurationManager.AppSettings["IpRoute"];
    ddlCom.Text = ConfigurationManager.AppSettings["Com"];
    ddlbote.Text = ConfigurationManager.AppSettings["Rate"];

    写config确怎么也写不进去,用了百度上很多方法,后来发现有时候能读到上次改的内容,但是config文件里却没有。

    后来得出点经验:

    1: winform运行时是在exe目录下copy个暂时的config文件,debug文件下的app.config可能是系统自带的,怎么都写不进去,得用后来生成的..demo.config读写(具体原理我也不太清楚)

    2.app.config路径是在debug文件里,而不是根目录的那个app.config,程序生成都是debug里生成的,所以根目录那个app.config更加不可能写进去。。

    最后的代码:

    private void BtnSubmit_Click(object sender, EventArgs e)
    {
    ExeConfigurationFileMap file = new ExeConfigurationFileMap();
    file.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
    Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
    var myApp = (AppSettingsSection)config.GetSection("appSettings");
    if (ComPanel.Visible == true)
    {
    myApp.Settings["Rate"].Value = ddlbote.Text;
    myApp.Settings["Com"].Value = ddlCom.Text;
    config.Save();
    MessageBox.Show("配置成功!");
    }
    if (IPpanel.Visible == true)
    {
    myApp.Settings["IpRoute"].Value = txtIp.Text;
    config.Save();
    MessageBox.Show("配置成功!");
    }

  • 相关阅读:
    oracle—数据泵及常用参数
    NTP服务及时间同步
    kudu安装
    ogg12c 配置
    ogg12-ERROR OGG-01031 file D:OGGdirdated000000 is not in any allowed output directories
    ogg12c_静默安装
    git的基本使用
    redis数据库
    linux之网络
    flask框架基础
  • 原文地址:https://www.cnblogs.com/clsl/p/10433746.html
Copyright © 2011-2022 走看看