【发布时间】:2019-12-08 02:06:26
【问题描述】:
我对配置文件有点陌生。我知道我可以添加键值对,并且可以随时访问和更改它们。我正在尝试在 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.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");
}
}
}
【问题讨论】:
-
你使用过这样的system.configuration.configurationmanager.openexeconfiguration吗?你是用 system.configuration 添加的吗?
-
@NastaranHakimi 我将代码添加到主要问题中,谢谢
-
我不确定但是如果你插入 system.configuration.configurationmanager.openexeconfiguration 而不是 ConfigurationManager.OpenExeConfiguration
-
我知道这是关于 appsettings 的,但也许对你有帮助 stackoverflow.com/questions/53414794/…
-
点击链接可能对你有帮助docs.microsoft.com/en-us/dotnet/api/…
标签: c# runtime app-config