无法在 app.config 上写入设置(或未显示任何更改) [英] Cannot write settings on app.config (or no changes are shown)

查看:38
本文介绍了无法在 app.config 上写入设置(或未显示任何更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 .net framework 4.5 上创建一个应用程序

每当我想加载应用程序配置值(appSettings 部分)并写入更改时,如果我刷新该部分,我将看不到我的更改.我在做什么错?我该如何解决?

代码

 private void LoadConfig(){NameValueCollection appSettings = ConfigurationManager.AppSettings;for (int i = 0; i < appSettings.Count; i++){开关 (appSettings.GetKey(i)){案例初始目录":txtInitialCatalog.Text = appSettings.GetValues(i)[0];休息;案例数据源":txtDatasource.Text = appSettings.GetValues(i)[0];休息;案例用户名":txtUsername.Text = appSettings.GetValues(i)[0];休息;案例密码":txtPassword.Text = appSettings.GetValues(i)[0];休息;案例portalUrl":txtUrl.Text = appSettings.GetValues(i)[0];休息;}}}私有无效 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 文件

<预><代码><配置><启动><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></启动><应用设置><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() 并在按下按钮时保存数据(确定或应用)我关闭应用程序我重新运行它但没有对 app.config 文件进行任何更改

有什么想法???

解决方案

我认为正在发生的事情是您正在 Visual Studio 中进行测试,它将 App.Config 复制到您的 Debug/Release 目录并将其重命名为 YourApplication.vshost.每次启动应用程序时都会重置 exe.config.尝试在 Visual Studio 之外运行可执行文件,它将使用 YourApplication.exe.config 文件,看看它是否适合你.如果我在 Visual Studio 之外运行代码,您的代码正在为我工​​作,并在应用程序重新启动时保留您的更改.

I'm creating an application on .net framework 4.5

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 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>

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

any ideas???

解决方案

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天全站免登陆