I am trying to update the app.config file at runtime. I get the error
System.NullReferenceException: object reference not set to an instance of an object. line 59.
What I am trying to do is change the url at runtime, by having a pop up form which has a textbox which is used for the url, this is then used to update the config file.
public void changeSettings()
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = config.AppSettings.Settings;
try
{
Console.WriteLine("nothing " + ConfigurationManager.AppSettings["client_postCodeRef_Service"]);
settings["client_postCodeRef_Service"].Value = textBox1.Text; <- line 59
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("applicationSettings");
Console.WriteLine("nothing 2 " + ConfigurationManager.AppSettings["client_postCodeRef_Service"]);
}
catch (ConfigurationErrorsException e)
{
MessageBox.Show("[Exception error: {0}]",
e.ToString());
}
}
here is the config file
<applicationSettings >
<Client.Properties.Settings>
<setting name="client_postCodeRef_Service" serializeAs="String">
<value>http://127.0.0.1/directory/directory/webService.asmx</value>
</setting>
</Client.Properties.Settings>
</applicationSettings>