如何在运行时更新app.config文件?

时间:2018-02-19 13:38:38

标签: c#

如何在运行时修改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");

0 个答案:

没有答案