0
votes

I am sort of new to config files. I am aware that I can add key value pairs and that it's possible to access them and change them on the fly. I am attempting to implement the ChangeConfiguration method on https://blogs.msdn.microsoft.com/youssefm/2010/01/21/how-to-change-net-configuration-files-at-runtime-including-for-wcf/

However, I am getting:

"'ConfigurationManager' does not contain a definition for 'OpenExeConfiguration'"

...and I get the same for trying to use ConfigurationManager.RefreshSection()

I am aware that the instructions date back to 2010 so by the looks of it, these instructions seem to no longer be the correct procedure to do this...?

Context

  • Web UI tests using Specflow, Selenium WebDriver, NUnit
  • Class Library targeting .NET Framework 4.6.1
  • Trying to add key value pairs at runtime in App.config

using System.Configuration; using System.Reflection;

namespace CoreSeleniumFramework.Managers
{
    public class ConfigurationManager
    {
        static void ChangeConfiguration()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            appSettings.Settings.Clear();
            appSettings.Settings.Add("name", "bar");
            config.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
1
did you use like this system.configuration.configurationmanager.openexeconfiguration? did you add using system.configuration?Nastaran Hakimi
@NastaranHakimi I added the code to the main question, thanksZuno
I am not sure but what if you insert system.configuration.configurationmanager.openexeconfiguration instead of ConfigurationManager.OpenExeConfigurationNastaran Hakimi
I know it is about appsettings but maybe helps you stackoverflow.com/questions/53414794/…Nastaran Hakimi
Follow the link may help you docs.microsoft.com/en-us/dotnet/api/…LDS

1 Answers

1
votes

Answered by @Nastaran Hakimi

Ok so this is a thing... need to use...

System.Configuration.ConfigurationManager.OpenExeConfiguration

...when it seems I should just need...

using System.Configuration;

Edit: if you get "object reference not set to an instance of an object" (or in other words, GetEntryAssembly() returns null), use GetCallingAssembly()