3

Im trying to creating a custom settings system for an administration and moderator panel that changes things like the site name, description, and so on. The method I'm using now is the NameValueCollection class that allows me to get the values of my defined keys within my Web.Config file to display in their proper places. My problem is, when it comes down to changing these values, I get Collection is read-only..... Heres a small snippet of the ActionResult in using to update the values and the definitions listed in my config.

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");

    NameValueCollection settings = new NameValueCollection();
    settings = (NameValueCollection)ConfigurationManager.GetSection("settings");

    settings.Set("siteName", SiteName);
    settings.Set("siteDesc", SiteDesc);

    return RedirectToAction("Index");

<section name="settings" type="System.Configuration.NameValueFileSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  <settings>
    <add key="siteName" value="Gizmo" />
    <add key="siteDesc" value="Welcome to a Gizmo Powered Website." />
  </settings>

Once it goes to settings.Set(); thats where the error begins. Is there anyway I get around this? Like an alternative way of updating the keys and values in my Web.Config?

Dezmen Ceo Sykes
  • 179
  • 1
  • 4
  • 16
  • in your example i don't see where you use your `config`, so you can remove it from this example – Epsil0neR May 22 '13 at 19:28
  • 1
    Are you trying to change the web.config of the same site where your code is running? – giacomelli May 22 '13 at 19:40
  • Look at [this post](http://stackoverflow.com/questions/719928/how-do-you-modify-the-web-config-appsettings-at-runtime). You can get some ideas from it. – Justjyde May 22 '13 at 19:55
  • Change your own web.congig will make your app to be restarted: http://stackoverflow.com/questions/613824/how-to-prevent-an-asp-net-application-restarting-when-the-web-config-is-modified – giacomelli May 22 '13 at 20:00
  • @giacomelli, If thats the case, is there a way I can make some custom config file like Web.config to do whats needed?? – Dezmen Ceo Sykes May 22 '13 at 20:04
  • Yes, you can use your won settings repository (xml file, database, etc) or try to use ConfigurationManager's Save method to create a separated config file (http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx). – giacomelli May 22 '13 at 20:22

1 Answers1

6

Firstly, you are creating instance of NameValueCollection and then immediately rewriting it. Secondly you are not using config variable. Thirdly use NameValueCollection.Remove() and Add() methods. It should do the trick...

See following example (it's very very similar to what you want to do)

        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        string key = "asd";
        var origValue = config.AppSettings.Settings[key];
        string newValue = origValue.Value + "changed";
        config.AppSettings.Settings.Remove(key);
        config.AppSettings.Settings.Add(key, newValue);

(And yes - every time you rewrite the web.config the application gets restarted.)

rocky
  • 7,506
  • 3
  • 33
  • 48