理解Visual Studio 2005/2008是如何管理User and Application settings的【转】

本文介绍如何使用Visual Studio 2005/2008进行.NET应用程序配置设置的管理。文章详细解释了用户设置与应用设置的区别,并展示了如何通过创建设置文件来轻松管理这些设置。
Understanding how User and Application settings are managed via Visual Studio 2005/2008

When you write a .NET application, to manage configuration settings is often required. These configuration settings can have a "user" or "application" scope.

User settings can be different for each user will use the application, instead application parameter is fixed and defined for the entire machine.

Visual Studio 2005 (and 2008 as well) allows, adding a "settings" item to the solution/project to manage easily this kind of settings.

To create a settings item you can use the context menu on a project and select "new item"->"Settings File".

Visual Studio generates a class derived from System.Configuration.ApplicationSettingsBase, and a "app.config" file that contains an XML where you can find default values for all settings you'll define.

As shown below, from Visual Studio, is trivial add a setting using the visual interface:

image

In this case I added an "Age" setting of integer type with default value of 35.

Each time I add a setting, Visual Studio does 2 operations:

(1) it modifies *.designer.cs file related to my settings adding:


 

 

 

 

 

 

 

 

 
 
 

(2) It modifies app.config file adding:


 

 

 

 
 
 

As you can seem the default value (35) is present in both class and .config files. Is this redundant? No! infact in this way the .exe or the .dll generated file already contains his default settings and can word without the external (global) configuration file.

This means that:

  • If I run the application without the corresponding .config file in his directory, the settings class will use the default setting provided inside the code
  • If I run the application with the .config file present in his directory, the application will use the value provided in the XML

So the usefullnes of .config file is clear where there is the requirement to override one of default value hard-coded in the application with a new applicatin wide setting.

Technorati Tag: VisualStudio , Settings

Published Wednesday, October 31, 2007 2:05 PM by NicolD

转载于:https://www.cnblogs.com/shanghaif/archive/2008/12/26/1362727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值