0

我正在用这个拔掉我的头发,改变了我的 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>
4

0 回答 0