0

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");
        }
    }
}
Zuno
  • 433
  • 1
  • 7
  • 17
  • 2
    did you use like this system.configuration.configurationmanager.openexeconfiguration? did you add using system.configuration? – Nastaran Hakimi Dec 07 '19 at 10:48
  • @NastaranHakimi I added the code to the main question, thanks – Zuno Dec 07 '19 at 10:49
  • I am not sure but what if you insert system.configuration.configurationmanager.openexeconfiguration instead of ConfigurationManager.OpenExeConfiguration – Nastaran Hakimi Dec 07 '19 at 10:53
  • I know it is about appsettings but maybe helps you https://stackoverflow.com/questions/53414794/im-unable-to-use-configurationmanager-appsettings-in-coded-ui – Nastaran Hakimi Dec 07 '19 at 11:07
  • Follow the link may help you https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.openexeconfiguration?view=netframework-4.8 – LDS Dec 07 '19 at 11:29

1 Answers1

1

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()

Zuno
  • 433
  • 1
  • 7
  • 17