4

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?

Ujjwal Vaish
  • 373
  • 1
  • 7
  • 21

1 Answers1

5

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

Community
  • 1
  • 1
Chander .k
  • 541
  • 4
  • 12
  • 1
    Yes it works but gives me a dialog box to continue because "some files have been changed". Anyway I can remove this? – Ujjwal Vaish Jun 30 '15 at 09:48
  • 1
    In developement mode it will give you dialog box when you will use it in IIS it will not give any dialogue. – Chander .k Jun 30 '15 at 10:02
  • 1
    When config file is saved/changed, won't this reset the app? – TheVillageIdiot Jun 30 '15 at 10:06
  • 1
    yes after changing webconfig. The application pool will restart and session state will be lost – Chander .k Jun 30 '15 at 10:07
  • 1
    When you say, session state will be restarted, does it mean the current value of all the variables will be lost. – Ujjwal Vaish Jun 30 '15 at 10:23
  • 1
    Yes Current session will be lost for all users which are logged in into the site. whether you are edit it by manually or programaticaly. – Chander .k Jun 30 '15 at 10:26
  • 1
    Okay, Thanks a ton. This helped. :) – Ujjwal Vaish Jun 30 '15 at 12:47
  • 2
    Yes @UjjwalVaish changing config file except from setup screens etc is quite foolish adventure. Even if you have some value that you may need to change frequently, instead of putting it in web.config put it in database and have a timed refresh of it or have a trigger on some page to refresh it from database. Else all your user will lost their unsaved work and be booted out. – TheVillageIdiot Jun 30 '15 at 13:29
  • 1
    Thanks guys, I have decided not to change the config file. I am updating info in a txt file and reading it from there the next time. – Ujjwal Vaish Jul 01 '15 at 05:45