许静外篇贩卖:Enterprise Library 4.1学习笔记1----配置应用程序块(c/s和b/s均适用) - 菩提树下的杨过.Net - 博客园

来源:百度文库 编辑:九乡新闻网 时间:2024/07/07 16:30:52

Enterprise Library 4.1学习笔记1----配置应用程序块(c/s和b/s均适用)

园子里TerryLee的Enterprise Library系列文章回顾与总结 http://www.cnblogs.com/Terrylee/archive/2006/08/01/464591.html已经写得很全面了,不过不是针对4.1版,一边看这一系列的文章学习,一边在4.1上摸索,准备写几篇学习笔记,今天先来认识Configuration Application Block(配置应用程序块)

参照TerryLee的文章,在4.1上怎么也找不到Configuration Application Block,相信很多人也跟我一样晕吧? 呵呵

无奈之下,打开\EntLib41Src\Quick Starts\Configuration-Migration\CS 运行了下,恍然大悟!

This QuickStart does not use Enterprise Library. It is intended toprovide guidance to users of previous versions of the Enterprise Library Configuration Application Block on how to migrate to the new features in System.Configuration in the .NET Framework 2.0.
TheConfiguration functionality in the Enterprise Library Core providesservices to the other blocks in the library and is not normally neededin user code.

大意是:Configuration Application Block这个模块现在已经合并到.Net 2.0 System.Configuration中了,所以EL中不再单独保留这一模块,也就是说这部分功能完全用.net 2.0自带的功能就可实现

应用场景:有时候,我们希望把某些类能序列化保存在app.config或web.config中,并能读写。

使用步骤:

1.先定义希望序列化保存的类,注意要继承自ConfigurationSection,示例代码如下:

Code
using System.Configuration;

namespace ConfigTest
{
    public class MyConfigClass : ConfigurationSection
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }


        [ConfigurationProperty("age")]
        public int Age
        {
            get { return (int)this["age"]; }
            set { this["age"] = value; }
        }


        public override string ToString()
        {
            return "Name=" + Name + ",Age=" + Age;
        }
    }
}

 

2.写入配置

const string SECTIONNAME = "MySettings";

private void btnWrite_Click(object sender, EventArgs e)
        {
            MyConfigClass _myConfig = new MyConfigClass();
            _myConfig.Age = int.Parse(txtAge.Text);
            _myConfig.Name = txtName.Text;

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.Sections.Remove(SECTIONNAME);
            config.Sections.Add(SECTIONNAME, _myConfig);
            config.Save();
        }注意:这是winform(c/s)下的代码,如果是网站web应用,这样是会出错的!Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);要改成下面这样string _ConfigPath = Server.MapPath("web");//如果这里web改为web.config,最终会生成一个新的web.config.config的文件,所以这里必须在根目录下新建一个名为"web"(注意不带扩展名)文本文件,然后系统才会正确写到web.config中,能想到这招骗过系统,我太有才啦^_^Configuration config = ConfigurationManager.OpenExeConfiguration(_ConfigPath);3.读取配置Code
private void btnRead_Click(object sender, EventArgs e)
        {
            //winform环境下,不加这一行,则永远读取的是缓存中的“旧”值;webform中因为页面刷新的关系,不加也可以正常读取到新的值
            ConfigurationManager.RefreshSection(SECTIONNAME);
            
            MyConfigClass configData = ConfigurationManager.GetSection(SECTIONNAME) as MyConfigClass;
            if (configData != null)
            {
                txtRead.Text = configData.ToString();
            }
            else 
            {
                txtRead.Text = SECTIONNAME + "配置节读取失败!";
            }

        }另外当配置更改(也就是配置值被修改)时,可以利用FileSystemWatcher监听实现触发某一事件,详情可见\EntLib41Src\Quick Starts\Configuration-Migration示例程序
Enterprise Library 4.1学习笔记1----配置应用程序块(c/s和b/s均适用) - 菩提树下的杨过.Net - 博客园 Enterprise Library 4.1学习笔记3----安全应用程序块 - 菩提树下的杨过.Net - 博客园 Enterprise Library 4.1学习笔记6----加密应用程序块 - 菩提树下的杨过.Net - 博客园 Enterprise Library Step By Step系列(一):配置应用程序块——入门篇 - TerryLee's Tech Space - 博客园 B\S和C\S的区别 B/S和C/S的区别 浅论C/S和B/S体系结构 IIS7上部署Asp.Net4.0时UrlRouting的若干问题 - 菩提树下的杨过.Net - 博客园 NET平台下WEB应用程序的部署(安装数据库和自动配置) 老徐的博客 ? Blog Archive ? 微软企业库(1):Enterprise Library 5.0介绍 重温delphi之:如何快速开发原生ActiveX控件 - 菩提树下的杨过.Net - 博... B/S架构,C/S 架构区别 (WEB)B/S类软件和(桌面)C/S类软件安全性及稳定性比较! CS架构和BS架构的区别,用qq、msn说明为什么用c/s不用b/s,用sohu为什么用b... vs2008下“由于应用程序的配置不正确,应用程序未能启 Petshop3.0学习笔记(四)应用程序接口层 - torome - 博客园 C#.net学习笔记之托管代码&非托管代码 学习资料大全 - 老排长的日志 - 网易博客s .NET (C#) Internals: ASP.NET 应用程序与页面生命周期 应用程序配置和动态加载1----程序集 Concept map Study-Flickr 概念图学习笔记 ? Seanmao's ... 学习笔记:ASP.NET MVC2 之验证 - 看花开花落 - 博客园 TN-C、TN-S和TN-C-S三种系统 有关TN-C、TN-S和TN-C-S三种系统常见问题及解答