Enterprise Library Step By Step系列(十六):使用AppSetting Application Block

简介:

Enterprise Library Step By Step系列(十六):使用AppSetting Application Block

Terrylee 2005 12 07
概述
AppSetting  Application  Block 并不是企业库里面的一个应用程序块。它可以使我们运用企业库的配置工具来管理类似于 appSetting 这样的配置文件中的标记,像我们经常用到的数据库连接字符串。
安装
1.    .NET 环境下编译 AppSetting 解决方案;
2.    拷贝如下两个 DLL Enterprise Library \bin   目录下
None.gif Kokuti.EnterpriseLibrary.AppSettings.dll
None.gif
None.gifKokuti.EnterpriseLibrary.AppSettings.Configuration.Desing.dll
None.gif
配置
1.    用企业库管理工具打开一个应用程序的配置文件,并创建一个 AppSetting  Application  Block
 
2.    点击 Collection 后面的按钮,   打开 Collection Edito r 对话框。
 
3.    添加一个新的名 - 值对到集合中。
 
4.    添加目录拷贝
None.gif copy  " $(ProjectDir)\*.config "   " $(TargetDir) "
使用
AppSetting  Application  Block 通过AppSettingsHelper可以很方便的进行操作,需要添加Kokuti.EnterpriseLibrary.AppSettings.dll到项目中。
1 .获取名-值对
1None.gifthis.rtb_Value.Text = AppSettingsHelper.AppSettings[this.rtb_Key.Text];
2 .设置名-值对
1 None.gif AppSettingsHelper.AppSettings[ this .rtb_Key.Text]  =   this .rtb_Value.Text;
3 .保存
1 None.gif AppSettingsHelper.Save();
4.   当配置文件的内容改变时,通知应用程序。
 1 ExpandedBlockStart.gif /// <summary>
 2InBlock.gif        /// 注册变更通知事件
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="sender"></param>
 5ExpandedBlockEnd.gif        /// <param name="e"></param>

 6 None.gif          private   void  Form1_Load( object  sender, System.EventArgs e)
 7 ExpandedBlockStart.gif         {
 8InBlock.gif            AppSettingsHelper.AppSettingsChangedEvent += new AppSettingsHelper.AppSettingsChangedDelegate(AppSettingsChangedHandler);
 9ExpandedBlockEnd.gif        }

10 None.gif        
11 ExpandedBlockStart.gif         /// <summary>
12InBlock.gif        /// 变更处理程序
13ExpandedBlockEnd.gif        /// </summary>

14 None.gif          private   void  AppSettingsChangedHandler()
15 ExpandedBlockStart.gif         {
16InBlock.gif            this.rtb_Value.Text = AppSettingsHelper.AppSettings[this.rtb_Key.Text];
17ExpandedBlockEnd.gif        }
Demo 运行界面
 
  小结
AppSetting  Application  Block 是比较简单的一个应用程序块,相信有前面其他应用程序块的使用经验,大家很快就能掌握,同时在这里向原作者 Robert Kokuti MCSD .Net 表示感谢。












本文转自lihuijun51CTO博客,原文链接: http://blog.51cto.com/terrylee/67619  ,如需转载请自行联系原作者

相关文章
|
Web App开发 SQL 移动开发
step by step guide tell you how to build a website like apkmirror
There are many [free apk download](https://idoras.com) websites such as apkmirror, today i will tell you how to build a website like apkmirror, the programming language i used is [node.js](https://nodejs.org/en/), the database i used is [mongodb](https://www.mongodb.com/), search engine used is [ela