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("配置成功!");
    }

  • 相关阅读:
    iOS exit(0); 直接退出程序
    友盟推送简单调用
    KxMenu下拉菜单
    打开相册另类写法
    简洁调用字号
    十六进制颜色宏
    Swift定义单例
    不要在初始化方法和dealloc方法中使用Accessor Methods
    copyin函数
    c语言中的赋值
  • 原文地址:https://www.cnblogs.com/clsl/p/10433746.html
Copyright © 2011-2022 走看看