App Settings in Visual Studio 2005

Everyday I’m learning new things about Visual Studio 2005 and version 2.0 of the .Net Framework. Most of the time when I “google” something (I like it when a company name becomes a word in the English language), I can quickly find several examples of what I want to do, and I quickly go about my business. However, now and then, there’s a topic for which I’m not able to find any really good examples. The new application settings feature in Visual Studio is, I believe, a case in point.

In the old days of Visual Studio 2003, there was a nice way to store application settings in a configuration file, so that the application could retrieve those settings at runtime. Someone, usually an administrator, could change the setting in the config file without the developer needing to recompile the application because the settings were contained in a separate file where they were stored as xml. As nice a feature as this was, the implementation in version 1.x of the Framework was someone limited. Mainly lacking was an API for writing settings to the file programmatically, and the settings were valid only for EXE’s and web apps, but not for individual DLL’s.

In Visual Studio 2005, the mechanism for application settings has been totally revamped. In VS 2003, there were “dynamic properties” for components, which could be set from the property sheet and were written to a config file. That has all been thrown out in VS 2005, replaced by a “Settings” property page for the project itself.

This means that config settings can apply not only to EXE’s and web apps, but also to individual DLL’s. VS 2005 provides a very nice editor for adding, changing and removing settings, and which allows you to designate a corresponding type (for example, int, string, Guid, DateTime, etc.). Visual Studio accomplished this feat by generating a settings class on the fly. The addition of strongly typed settings gives compile time type safety, which was lacking by default in VS 2003. Also, settings are scoped both at the Application level, and at the User level. So it’s possible to maintain settings that are specific to a particular user, such as the most recent position of the app’s main window, or other similar settings.

By the way, VS 2005 still generates an app.config file, which in turn is copied to a file with the same name as the assembly and placed in the same folder. The config file is a bit more sophisticated, with separate sections for application and user settings.

So what code do you write to retrieve these settings? That’s where my Google search came up a bit sparse. I did find one article, but it did not address separate app settings for DLL’s and the code snippet only showed how to enumerate settings, but not how to retrieve individual settings. So here’s how to do it:

// Get Project Settings
Properties. Settings settings = Properties.Settings.Default;

// Get App Settings
string strSetting = settings.StringSetting;
DateTime dateSetting = settings.DateSetting;
Guid guidSetting = settings.GuidSetting;
int intSetting = settings.IntSetting;

The Settings class in the Properties namespace is automatically generated by the Visual Studio IDE. Application scoped settings are read-only, while User scoped settings are read-write and are specific to the logged on user.

Click here for a sample application I created which demonstrates the new application settings feature of Visual Studio 2005, including settings used by a DLL.

About Tony Sneed

Married with three children.
This entry was posted in Technical. Bookmark the permalink.

5 Responses to App Settings in Visual Studio 2005

  1. Vipin says:

    Hi, Please tell me how can I add app.congfig in my windows application through Visual Sutdio 2005, As I am unable to see any option to add the same where click the Add New….

  2. Tony says:

    It’s there. The item is called “Application Configuration File”. In VS 2005 it’s hard to find because items are not sorted alphabetically, but in VS 2008 they are. In this day and age, you should be using VS 2008 anyway, because it can target .NET 2.0 apps.

  3. parmita says:

    and both are present in VS 2008 inside web.config file
    My question is :
    Is absent in web.config file in VS 2005 ?

  4. parmita says:

    appsettings and configuration elements are present in VS 2008

    I want to know whether appsettings is absent in a web.config file using VS 2005?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s