注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 IB客座主编(四)美国西蒙公..
 帮助

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


2005-12-08 09:51:00
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terrylee.blog.51cto.com/342737/67619

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

Terrylee20051207
概述
AppSetting  Application  Block并不是企业库里面的一个应用程序块。它可以使我们运用企业库的配置工具来管理类似于appSetting这样的配置文件中的标记,像我们经常用到的数据库连接字符串。
安装
1.   .NET环境下编译AppSetting解决方案;
2.   拷贝如下两个DLLEnterprise Library \bin 目录下
Kokuti.EnterpriseLibrary.AppSettings.dll

Kokuti.EnterpriseLibrary.AppSettings.Configuration.Desing.dll
配置
1.   用企业库管理工具打开一个应用程序的配置文件,并创建一个AppSetting  Application  Block
 
2.   点击Collection后面的按钮, 打开Collection Editor对话框。
 
3.   添加一个新的名-值对到集合中。
 
4.   添加目录拷贝
copy "$(ProjectDir)\*.config" "$(TargetDir)"
使用
AppSetting  Application  Block通过AppSettingsHelper可以很方便的进行操作,需要添加Kokuti.EnterpriseLibrary.AppSettings.dll到项目中。
1.获取名-值对
1this.rtb_Value.Text = AppSettingsHelper.AppSettings[this.rtb_Key.Text];
2.设置名-值对
1AppSettingsHelper.AppSettings[this.rtb_Key.Text] = this.rtb_Value.Text;
3.保存
1AppSettingsHelper.Save();
4.  当配置文件的内容改变时,通知应用程序。
 1/// <summary>
 2        /// 注册变更通知事件
 3        /// </summary>
 4        /// <param name="sender"></param>
 5        /// <param name="e"></param>

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

10        
11        /// <summary>
12        /// 变更处理程序
13        /// </summary>

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

本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67619





    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: