1、配置文件监听
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.IO; namespace CSharpStudy { public sealed class ConfigMonitor { /// <summary> /// file path /// </summary> private static string filePath = string.Empty; /// <summary> /// file map /// </summary> private static ExeConfigurationFileMap configFileMap; /// <summary> /// constructor /// </summary> static ConfigMonitor() { Console.WriteLine("constructor..."); MonitorConfigFile(); InitConfigInfo(); } /// <summary> /// after change config delegate /// </summary> public delegate void AfterConfigModifyEvent(); /// <summary> /// after change config event /// </summary> public static event AfterConfigModifyEvent afterConfigModifyEvent; /// <summary> /// config object /// </summary> public static Configuration Config { get; set; } /// <summary> /// monitor config file /// </summary> public static void MonitorConfigFile() { Console.WriteLine("MonitorConfigFile..."); // don‘t throw exception try { FileSystemWatcher fileWatcher = new FileSystemWatcher(); fileWatcher.Path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; fileWatcher.Filter = "ParamSettings.config"; // add event handlers fileWatcher.Changed += new FileSystemEventHandler(OnChanged); fileWatcher.Created += new FileSystemEventHandler(OnChanged); fileWatcher.Deleted += new FileSystemEventHandler(OnChanged); fileWatcher.Renamed += new RenamedEventHandler(OnChanged); fileWatcher.EnableRaisingEvents = true; } catch { } } public static void OnChanged(object source, FileSystemEventArgs e) { InitConfigInfo(); RaiseEvent(); } /// <summary> /// init all config info /// </summary> public static void InitConfigInfo() { Console.WriteLine("InitConfigInfo..."); try { // don‘t throw exception filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\ParamSettings.config"; configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = filePath; Config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); } catch { } } /// <summary> /// raise event /// </summary> private static void RaiseEvent() { Console.WriteLine("RaiseEvent..."); if (afterConfigModifyEvent != null) { afterConfigModifyEvent(); } } } }
2、SettingsFactory
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharpStudy { public class SettingsFactory { static SettingsFactory() { ConfigMonitor.MonitorConfigFile(); } public static string ApplicationName { get { return GetAppSetting("ApplicationName"); } } private static string GetAppSetting(string key) { return ConfigMonitor.Config.AppSettings.Settings[key].Value; } } }
3、测试代码
public static void TestConfigFile() { while (true) { string str = Console.ReadLine(); if (str == "-1") { break; } Console.WriteLine(SettingsFactory.ApplicationName); } }
4、运行截图
4.1 初始化
说明:
在输入回车后,打印SettingsFactory.ApplicationName信息,
第一、SettingsFactory构造函数初始化
(1)、ConfigMonitor构造函数初始化,执行构造函数里面的方法
打印:
contructor...
MonitorConfigFile...
InitConfigInfo...
(2)、执行ConfigMonitor.MonitorConfigFile方法
打印:MonitorConfigFile...
第二、执行SettingsFactory.ApplicationName方法,打印:CSharpStudy
4.2 使用值
说明:再输入一次回车,结果打印:CSharpStudy。静态构造器,只在第一次使用该类的时候进行初始化。
4.3 程序运行时修改配置文件
找到项目所在目录下的:bin\Debug\ParamSettings.config配置文件,
将ApplicationName的Value修改为:测试项目
执行了四次OnChanged方法。
输入回车后
5、配置文件ParamSettings.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ApplicationName" value="CSharpStudy"/> </appSettings> </configuration>
时间: 2024-10-11 13:33:06