惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
博客园 - 司徒正美
美团技术团队
Martin Fowler
Martin Fowler
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
U
Unit 42
Y
Y Combinator Blog
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
GbyAI
GbyAI
H
Help Net Security
量子位
Last Week in AI
Last Week in AI
博客园_首页
腾讯CDC
小众软件
小众软件

博客园 - 冰绿茶

解决Ehcache缓存警告问题 关于NHibernate、Castle和WCF做分布式事务时发生异常的解决办法 数据绑定控件和XmlDataSource控件结合使用,通过后台绑定Xml数据片段遇到的问题 微软StockTrader 2.03 学习笔记(8)--配置服务实现示例指南(四) 微软StockTrader 2.03 学习笔记(7)--配置服务实现示例指南(三) 微软StockTrader 2.03 学习笔记(6)--配置服务实现示例指南(二) 微软StockTrader 2.03 学习笔记(5)--配置服务实现示例指南(一) 微软StockTrader 2.03 学习笔记(4)--配置数据库生成工具介绍 SQLServer实战经验分享--ServiceBroker安全配置和使用示例 ASP.NET Trick文章系列--使用State Server管理Session状态的另类经济用法 微软StockTrader 2.03 学习笔记(3)--配置网站和配置服务在StockTrader中的使用示例 在数据库事务设计中经常会遇到的疑惑 微软StockTrader 2.03 学习笔记(2)--什么是配置网站和配置服务、配置存储库 微软StockTrader 2.03 学习笔记(1)--学习大纲整理 asp.net 2.0页面性能的考虑--异步页面处理模型 Asp.net 2.0 动态加载其他子目录用户控件问题 Web 下配置文件信息的读写 ASP.NET 2.0加密网站配置文件中的信息 - 冰绿茶 - 博客园 Understanding ASP.NET Provider Model (Creating Custom Membership and Role Providers) - Part 3
.net 2.0 中对配置文件的读写
冰绿茶 · 2006-02-21 · via 博客园 - 冰绿茶

在基于 .net 2.0 的企业库中,原来的配置应用程序块被废除了,使用了 .net 2.0 自带的读写配置功能,下面我们就来看看 .net 2.0 中读写配置的功能。

即:  ConfigurationManager  类

注意:
ConfigurationManager 是处理客户端应用程序配置文件的首选方法;不推荐使用任何其他方法。
对于 Web 应用程序,建议使用 WebConfigurationManager 类。

这个类的  AppSettings 属性 在以前1.0 的时候,就有了, 2.0 中增加了 ConnectionStrings 属性。
这些都不是今天我们要探讨的内容,我们今天要探讨的内容,是把一个配置类保存到配置文件中,以及把这个配置类从配置文件中实例化出来。

这个配置类,必须是 派生自
System.Configuration.ConfigurationSection 类

如下面的类就是一个配置类

using System.Text;
using System.Configuration;
namespace ConfigTest
{
    class ConfigDataClass : ConfigurationSection
    {
        public ConfigDataClass()
        { }

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

        [ConfigurationProperty("name")]
        public string Name{
            get{ return this["name"].ToString();}
            set{ this["name"] = value;}
        }

        public override string ToString(){
            StringBuilder info = new StringBuilder();
            info.AppendFormat("id = {0};name = {1}", ID, Name);
            return info.ToString();
        }
    }
}


先说如何把这个配置类更新到配置文件中

// 配置信息类初始化
ConfigDataClass configData = new ConfigDataClass();
configData.ID = 100;
configData.Name = "我是谁?";

// 打开当前文件的配置文件
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 干掉原先的配置
config.Sections.Remove("SettingsData");
// 把新的配置更新上去
config.Sections.Add("SettingsData", configData);
// 保存配置文件
config.Save();

MessageBox.Show(configData.ToString());


读取配置信息

ConfigDataClass configData = ConfigurationManager.GetSection("SettingsData") as ConfigDataClass;
if (configData == null) return;
MessageBox.Show(configData.ToString());

当文件修改的时候,自动从新登录配置文件需求

这个更简单,只需要使用一个 System.IO.FileSystemWatcher 对象即可
private FileSystemWatcher watcher;

在初始化的时候,订阅文件改变事件。

// Initialize file system watcher
watcher = new FileSystemWatcher(AppDomain.CurrentDomain.BaseDirectory);
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = false;

然后在 watcher_Changed 方法中,

private void watcher_Changed(object sender, FileSystemEventArgs e)
{
    if (e.FullPath.ToLower().Contains(".config"))
    {
  for (int i = 0; i < 3; i++)
  {
   try
   {
    // Using the static method, read the cached configuration settings
    ConfigurationManager.RefreshSection("EditorSettings");
    break;
   }
   catch (ConfigurationErrorsException)
   {
    if (i == 2) throw;
    else Thread.Sleep(100);
   }
  }
    }
}

显然,上述的功能已经能满足我们的需求了,所以企业库才废弃了之前的配置管理应用程序块。