Personally, I use the appSettings section of the web.config file a lot more than I use application variables. If you're just starting out with .NET development, I'd say use appSettings for relatively static values (i.e. an SMTP server for sending email, or a path to a particular directory for uploading files, etc...things that you probably won't change). If you anticipate the values changing a lot (or even a little) during runtime, it would be easier to set initial values in an application variable in global.asax and modify it whenever you need to.
Having said that, you CAN programatically change the appSettings section of web.config, and could even write a wrapper class to abstract some of the relative complexities of doing it (it's not that hard and probably only a few lines of code), but typing: Application["VariableName"] = "Something else" is just easier when you're learning. To get started, I'd probably just do as I outlined above and as you get a little more familiar with .NET, you can branch out into a more in depth solution.
In case you're curious as to what it would look like to update an appSetting value programmatically, take a look at this:
public void Update(string key, string value)
{
Configuration configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection appSettingsSection =
(AppSettingsSection)configuration.GetSection("appSettings");
if (appSettingsSection != null)
{
appSettingsSection.Settings[key].Value = value;
config.Save();
}
}
Scott M Schluer
---
MCPD: ASP.NET Developer 3.5
