Cannot write settings on app.config (or no changes are shown)

半腔热情 提交于 2019-12-10 15:17:43

问题


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


回答1:


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.




回答2:


usually, I do this:

 ...
 config.AppSettings.Settings.Remove("TextBoxNumber");
 config.AppSettings.Settings.Add("TextBoxNumber", "");

It works fine. I guess it is because one of your key is not in the app.config yet. You need Remove/Add it (like the above code), or, create the key in the app.config first.

==================== Update

Try this:

 config.AppSettings.Settings["initialCatalog"].Value = txtInitialCatalog.Text;

instead of

 appSettings["initialCatalog"].Value = txtInitialCatalog.Text;

Any difference?



来源:https://stackoverflow.com/questions/13130620/cannot-write-settings-on-app-config-or-no-changes-are-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!