Changing App.config at Runtime

后端 未结 4 1943
旧时难觅i
旧时难觅i 2020-12-01 13:29

I\'m writing a test WinForms / C# / .NET 3.5 application for the system we\'re developing and we fell in the need to switch between .config files at runtime, but this is tur

相关标签:
4条回答
  • 2020-12-01 14:03

    Assuming that the file handle to the configuration file is closed after the configuration file has been read and processed, I would send a message to the application to tell it to re-read the configuration file after you have updated the file. If this approach is not working, then I suspect (as Hogan suggested) that the file handle is not closed. What error codes are you getting from the file opening, reading and closing system calls? (use perror to report the error message)

    0 讨论(0)
  • 2020-12-01 14:11

    I understand this is quite an old thread, but I could not get the listed methods to work. Here is a simpler version of the UpdateAppSettings method (using .NET 4.0):

    private void UpdateAppSettings(string theKey, string theValue)
            {
                Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (ConfigurationManager.AppSettings.AllKeys.Contains(theKey))
                {
                    configuration.AppSettings.Settings[theKey].Value = theValue;
                }
    
                configuration.Save(ConfigurationSaveMode.Modified);
    
                ConfigurationManager.RefreshSection("appSettings");
            }
    

    Pretty readable and avoids having to traverse the app.config using Xpath or the like. Note: The code above is inspired from this snippet on MSDN.

    0 讨论(0)
  • 2020-12-01 14:11

    My guess is you are not really closing the file handle the first time so windows does not "see" you making the later changes.

    My suggestions is to use an API call to IIS and turn off the web app (and pool), make the change, turn on the web app. This way you are sure it will re-read the file and have a "clean" environment for each test.

    0 讨论(0)
  • 2020-12-01 14:20

    UPDATE

    The solution below did not work because XmlDocument does not dispose and it seems some versions of .net do not close correctly when given a file path. The solution (example code in the link) is to open a stream which will do a dispose and pass that stream to the save function.

    A solution is shown here. http://web-beta.archive.org/web/20150107004558/www.devnewsgroups.net/group/microsoft.public.dotnet.xml/topic40736.aspx


    Old stuff below

    Try this:

    Note, I changed to xpath, but it has been a while so I might have gotten the xpath wrong, but in any case you should use xpath and not walk the tree. As you can see it is much clearer.

    The important point is the using statement which will dispose(), which I think was your problem.

    Let me know, good luck.

      public void UpdateAppSettings(string key, string value)
      {
        using (XmlDocument xmlDoc = new XmlDocument())
        {
          xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
          xmlDoc.DocumentElement.FirstChild.SelectSingleNode("descendant::"+key).Attributes[0].Value = value;
          xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }
        System.Configuration.ConfigurationManager.RefreshSection("section/subSection");
      }
    
    0 讨论(0)
提交回复
热议问题