为什么ConfigurationManager不包含OpenExeConfiguration的定义? C#

问题描述 投票:0回答:1

我是配置文件的新手。我知道我可以添加键值对,并且可以随时访问和更改它们。我正在尝试在https://blogs.msdn.microsoft.com/youssefm/2010/01/21/how-to-change-net-configuration-files-at-runtime-including-for-wcf/

上实现ChangeConfiguration方法

但是,我得到:

“'ConfigurationManager'不包含'OpenExeConfiguration'的定义”

...和尝试使用ConfigurationManager.RefreshSection()的方式相同。>

我知道这些说明可以追溯到2010年,因此从外观上看,这些说明似乎不再是执行此操作的正确程序...?

上下文
  • 使用Specflow,Selenium WebDriver,NUnit的Web UI测试

  • 面向.NET Framework 4.6.1的类库
  • 尝试在运行时在App.config中添加键值对
  • 使用System.Configuration;使用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");
            }
        }
    }
    

    我是配置文件的新手。我知道我可以添加键值对,并且可以随时访问和更改它们。我正在尝试实现ChangeConfiguration ...

    c# runtime app-config
    1个回答
    0
    投票

    由@Nastaran Hakimi回答

    © www.soinside.com 2019 - 2024. All rights reserved.