不能写的app.config设置(或没有变化如图) [英] Cannot write settings on app.config (or no changes are shown)

查看:166
本文介绍了不能写的app.config设置(或没有变化如图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建.NET框架4.5

I'm creating an application on .net framework 4.5

每当我要加载的应用程序的配置值(的appSettings部分),并写了变化,我没有看到我的变化,如果我刷新部分。我在做什么错了?我怎么能解决呢?

whenever i want to load the app config values (appSettings section) and write the changes, i don't see my changes if i refresh the section. What i am doing wrong? and how can i solve it?

code

    private void LoadConfig()
    {
        NameValueCollection appSettings = ConfigurationManager.AppSettings;
        for (int i = 0; i < appSettings.Count; i++)
        {
            switch (appSettings.GetKey(i))
            {
                case "initialCatalog":
                    txtInitialCatalog.Text = appSettings.GetValues(i)[0];
                    break;
                case "dataSource":
                    txtDatasource.Text = appSettings.GetValues(i)[0];
                    break;
                case "userName":
                    txtUsername.Text = appSettings.GetValues(i)[0];
                    break;
                case "password":
                    txtPassword.Text = appSettings.GetValues(i)[0];
                    break;
                case "portalUrl":
                    txtUrl.Text = appSettings.GetValues(i)[0];
                    break;
            }
        }
    }

    private void SaveConfig()
    {
        System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
        appSettings["initialCatalog"].Value = txtInitialCatalog.Text;
        appSettings["dataSource"].Value = txtDatasource.Text;
        appSettings["userName"].Value = txtUsername.Text;
        appSettings["password"].Value = txtPassword.Text;
        appSettings["portalUrl"].Value = txtUrl.Text;
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }

App.config文件

App.config file

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="initialCatalog" value="a" />
    <add key="dataSource" value="b" />
    <add key="password" value="c" />
    <add key="userName" value="d" />
    <add key="portalUrl" value="e" />
    <add key="poolingTime" value="60000" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>

我叫ReadConfig()时,形式被激活并保存数据时,一个按钮pssed $ P $(确定或应用),我收我重新运行它的应用程序,但不进行任何更改到app.config文件

i call ReadConfig() when the forms is Activated and save the data when a button is pressed (Ok or Apply) i close the application i rerun it but no changes are made to the app.config file

任何想法?

推荐答案

我认为正在发生的事情是,你正在测试在Visual Studio中,它copys在app.config到您的调试/ Release目录,删除等YourApplication.vshost。 exe.config它得到每次启动应用程序复位。尝试运行可执行文件之外的Visual Studio将使用YourApplication.exe.config文件,看看是否适合您。您的code为我工作,并保持你对应用程序重新启动的变化,如果我运行它的Visual Studio之外。

I think what is happening is that you are testing in Visual Studio, it copys the App.Config to your Debug/Release directory and renames it YourApplication.vshost.exe.config which gets reset each time you start your application. Try running the executable outside of Visual Studio which will use the YourApplication.exe.config file and see if that works for you. Your Code is working for me and retaining your changes on application restart if I run it outside of Visual Studio.

这篇关于不能写的app.config设置(或没有变化如图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆