ConfigurationManager.RefreshSection(“appSettings”)不更新配置文件

时间:2012-06-11 08:53:42

标签: c# web-services config appsettings configurationmanager

我正在用这个改变我的头发,改变了我的app.settings url它不是从磁盘读取,因此它崩溃了我的应用程序,因为它使用的是缓存版本。我已经阅读了很多例子,它们为人们工作但却无法解决为什么这样做不起作用

private void button1_Click(object sender, EventArgs e)
    {
        changeSettings();
        ConfigurationManager.RefreshSection("appSettings");
        this.Close();
    }
    public void changeSettings()
    {
        Configuration config =
        ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        KeyValueConfigurationCollection settings = config.AppSettings.Settings;
        settings.Clear();
        //MessageBox.Show(settings["client_postCodeRef_Service"].Value);
        try
        {

            //settings["client_postCodeRef_Service"].Value = textBox1.Text;
            //ser.Url = settings["client_postCodeRef_Service"].Value;
            settings.Add("client_postCodeRef_Service", textBox1.Text);
            AppSettingsSection sect = (AppSettingsSection)config.GetSection("appSettings");
            ser.Url = sect.Settings["client_postCodeRef_Service"].Value;
            config.Save(ConfigurationSaveMode.Modified);
            MessageBox.Show(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            MessageBox.Show(settings["client_postCodeRef_Service"].Value);

        }
        catch (ConfigurationErrorsException e)
        {
            MessageBox.Show("[Exception error: {0}]",
                e.ToString());
        }

     } // end change settings

这是我的exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089" >
        <section name="Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <section name="client.Properties.Settings"  type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
 </configSections>
 <appSettings>
 <add key="client_postCodeRef_Service"  value="http://127.0.0.1/directory/directory/webService.asmx"/>  
 </appSettings>
 <system.serviceModel>
    <bindings />
    <client />
 </system.serviceModel>
 </configuration>

0 个答案:

没有答案