Joseph Michael Pesch
VP Programming

Override Application Settings C#

by 31. August 2010 17:18

When you create/edit application settings via the projects properties window in Visual Studio (as shown in screen shot below), it will create app/web.config entries (as shown below in sample .config entry below).  However, if you change those entries in the .config file on a deployed app/web instance, you may notice the updated .config values are not actually being used by the application/web when it runs.  In order to have the application/web use the values from your .config file you can write a custom handler (as shown in the sample code below).

Screen shot of properties dialog:

Sample of .config values entered via the propeties dialog:

<applicationSettings> 

  <MyApplication.Properties.Settings> 

    <settingname="MyUserSetting"serializeAs="String">

      <value>white</value>

    </setting>

  </ MyApplication.Properties.Settings>

</applicationSettings>

Sample of code to override the property:

<appSettings>

  <add key="MyackgroundColor"value="white"/>

</appSettings>

 

 

using System;

using System.Configuration;

using System.Drawing;

 

publicclass MyUserSettings : ApplicationSettingsBase

{

  [ApplicationScopedSetting()]

  [DefaultSettingValue("white")]

  public Color BackgroundColor //Note: property name (i.e. BackgroundColor) is arbitrary

  {

    get

    {

      return ((Color)ConfigurationManager.AppSettings["MyBackgroundColor"]);

    }

    set

    {

      this["BackgroundColor"] = (Color)value;

    }

  }

}

 

 

Tags:

ASP.Net | C#

Comments are closed