我正在尝试在运行时更新 app.config 文件。我得到错误
System.NullReferenceException:对象引用未设置为对象的实例。第 59 行。
我想要做的是在运行时更改 url,通过弹出一个表单,该表单有一个用于 url 的文本框,然后用于更新配置文件。
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());
}
}
这是配置文件
<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>