在这篇文章中,我将向你展示如何动态地管理c# asp.net中的xml配置文件。要动态地配置web.config文件,使用webconfigurationManager打开web配置文件,并检查键是否存在。如果appsetting没有被配置,添加一个新的键,否则更新appsetting。

protected void Page_Load(object sender, EventArgs e)
{
	UpdateSetting("UnobtrusiveJavaScriptEnabled", "true");
}

public void UpdateSetting(string key, string value)
{
	Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
	if (config.AppSettings.Settings[key] == null)
	{
		config.AppSettings.Settings.Add(key, value);
	}
	else
	{
		config.AppSettings.Settings[key].Value= value;
	}
	config.Save();
	ConfigurationManager.RefreshSection("appSettings");
}

运行应用程序,上述代码将更新属性 “UnobtrusiveJavaScriptEnabled “为true,并保存配置文件,然后刷新web.config文件中的appSettings节点。

(adsbygoogle = window.adsbygoogle || []).push({});