Reputation: 375
I am trying to update some configuration settings at run time via my c# code. This is my section of the web.config
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="URL" value="google.com"/>
<add key="Domain" value="d"/>
<add key="Project" value="p"/>
</appSettings>
And this is the code that I am using :
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
System.Configuration.ConfigurationManager.AppSettings.Remove("URL");
System.Configuration.ConfigurationManager.AppSettings.Add("URL","www.stackoverflow.com");
config.Save();
However , it is giving the error that my config file is read-only. I am using Visual Studio 2013. How should I fix this?
Upvotes: 4
Views: 10948
Reputation: 541
Will you please try this ?
protected void EditConfigButton(object sender, EventArgs e)
{
Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
//Edit
if (objAppsettings != null)
{
objAppsettings.Settings["test"].Value = "newvalueFromCode";
objConfig.Save();
}
}
Or Please refer link Editing Web.config programatically
Upvotes: 5