如何在运行时修改app.config文件的ServerURL值?
这是app.config文件:
<applicationSettings>
<SystemVerification.Settings>
<setting name="ServerURL" serializeAs="String">
<value>https://linkhere</value>
</setting>
</applicationSettings>
我试过了:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement element in xmlDoc.DocumentElement)
{
if (element.Name.Equals("applicationSettings"))
{
foreach (XmlNode node in element.ChildNodes)
{
if (node.Attributes[0].Name.Equals("ServerURL"))
{
node.Attributes[1].Value = "New Value";
}
}
}
}
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("applicationSettings");