• Enterprise Library 4.1 Configuration Sources 图文笔记


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

    image

    一,下载并安装好Enterprise Library 4.1

    二,新建一个Web应用程序

    三,右键点击Web.Config 文件 使用 Edit Enterprise Library Configuration 可以编辑Web.Config 但这里我没有无需编辑它

    image

    四,新建一个待保存的类型,下面是官方的示例程序

    using System.Text;
    using System.Configuration;
    
    namespace ConfigurationBlock
    {
        /// <summary>
        /// Summary description for ConfigurationData.
        /// </summary>
        public class EditorFontData : ConfigurationSection 
        {        
    
            public EditorFontData()
            {          
            }
    
            [ConfigurationProperty("name")]
            public string Name 
            {
                get { return (string)this["name"]; }
                set{ this["name"] = value; }
            }
    
            [ConfigurationProperty("size")]
            public float Size 
            {
                get{ return (float)this["size"]; }
                set{ this["size"] = value; }
            }
    
            [ConfigurationProperty("style")]
            public int Style 
            {
                get { return (int)this["style"]; }
                set{ this["style"] = value; }
            } 
    
            public override string ToString() 
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Name = {0}; Size = {1}; Style = {2}", Name, Size.ToString(), Style.ToString());
    
                return sb.ToString();
            }
        }
    }

    五,在页面中拖放按钮和文本框,并编写事件,保存配置信息的时候会生成一个新的配置文件来保存web.config.config  这样的格式,这是为了避免删除掉原有配置文件而新建的吧。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.IO;
    
    namespace ConfigurationBlock
    {
        public partial class _Default : System.Web.UI.Page
        {
            const string SECTIONNAME = "EditorSettings";
            private FileSystemWatcher watcher;
            Configuration config;
            protected void Page_Load(object sender, EventArgs e)
            {
                config = ConfigurationManager.OpenExeConfiguration(Server.MapPath("Web.config"));
        
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                
                EditorFontData configData = new EditorFontData();
                configData.Name = this.TextBox2.Text;
                configData.Size = 19;
                configData.Style = 9;
                
                config.Sections.Remove(SECTIONNAME);
                config.Sections.Add(SECTIONNAME, configData);
                config.Save();
    
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                GetSection();
            }
            private void GetSection()
            {
                //winform环境下,不加这一行,则永远读取的是缓存中的“旧”值;webform中因为页面刷新的关系,不加也可以正常读取到新的值
                ConfigurationManager.RefreshSection(SECTIONNAME);//清空缓存
                
    
                EditorFontData configData = config.GetSection(SECTIONNAME) as EditorFontData;
                if (configData != null)
                {
                    this.TextBox1.Text = configData.ToString();
                }
                else
                {
                    this.TextBox1.Text = SECTIONNAME + "配置节读取失败!";
                }
            
            }
    
        }
    }

    示例源码下载:EL41Sample.rar
    Enterprise Library 4.1 目录:Enterprise Library 4.1 快速使用图文笔记 目录

    冯瑞涛
  • 相关阅读:
    Delphi 2009增强之Exit函数
    带小数的10进制转16进制
    产生指定长度的随机字符串
    在delph 2009中,利用Build Events调用UPX
    WMI信息获取
    MYSQL 存储过程学习笔记
    将窗体透明化
    倒计时
    通过程序开启XP的ClearType显示效果
    使用ODAC调用ORACLE的自定义函数和存储过程
  • 原文地址:https://www.cnblogs.com/finehappy/p/1577783.html
Copyright © 2020-2023  润新知