A-A+

ConfigMonitor

2015年09月20日 C#基础知识 ConfigMonitor已关闭评论 阅读 219 次

/// <summary>
/// ConfigMonitor类
/// </summary>
public class ConfigMonitor
{
/// <summary>
/// 连接配置 Configuration
/// </summary>
private static Configuration policyReceiveConfig;

/// <summary>
/// 连接配置文件 ExeConfigurationFileMap
/// </summary>
private static ExeConfigurationFileMap map;

/// <summary>
/// 构造函数:初始化读取配置文件,为文件注册侦听器
/// </summary>
static ConfigMonitor()
{
MonitorConfigFile();
InitConnectionConfig();
}

/// <summary>
/// 配置文件变更代理 delegate
/// </summary>
public delegate void EventHandlerAfterConfigModify();

/// <summary>
/// 配置文件变更事件 event
/// </summary>
public static event EventHandlerAfterConfigModify ConfigModifyInfoEvent;

/// <summary>
/// 配置对象 Configuration
/// </summary>
public static Configuration ResourceScheduleConfig
{
get
{
return policyReceiveConfig;
}

set
{
policyReceiveConfig = value;
}
}

/// <summary>
/// 创建配置文件发动监听器
/// </summary>
public static void MonitorConfigFile()
{
FileSystemWatcher fileWatcher = new FileSystemWatcher();
fileWatcher.Path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\";
fileWatcher.Filter = "InterResourceScheduleService.config";
fileWatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName;

// Add event handlers.
fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
fileWatcher.Created += new FileSystemEventHandler(OnChanged);
fileWatcher.Deleted += new FileSystemEventHandler(OnChanged);
fileWatcher.Renamed += new RenamedEventHandler(OnChanged);

// Begin watching.
fileWatcher.EnableRaisingEvents = true;
}

/// <summary>
/// 保存AppSettings,注释内容不会丢失
/// </summary>
/// <param name="key">key</param>
/// <param name="value">value</param>
public static void SaveAppSettingsMethod(string key, string value)
{
XmlDocument xml = new XmlDocument();
string filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\InterResourceScheduleService.config";
xml.Load(filePath);
XmlNodeList nodeList = xml.GetElementsByTagName("appSettings");
if (nodeList != null)
{
if (nodeList.Count >= 1)
{
XmlNode node = nodeList[0];
foreach (XmlNode item in node)
{
if (item.NodeType == XmlNodeType.Comment)
{
continue;
}

XmlAttribute xakey = item.Attributes["key"];
XmlAttribute xmlValue = item.Attributes["value"];
if (xakey != null && xmlValue != null && xakey.Value.Equals(key))
{
xmlValue.Value = value;
}
}
}
}

xml.Save(filePath);
}

/// <summary>
/// 向订阅者发布信息(配置文件变更时)
/// </summary>
private static void RaiseEvent()
{
if (ConfigModifyInfoEvent != null)
{
ConfigModifyInfoEvent();
}
}

/// <summary>
/// 初始化数据库连接配置
/// </summary>
private static void InitConnectionConfig()
{
// 读取配置文件进行所有的连接初始化操作
map = new ExeConfigurationFileMap();
map.ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\InterResourceScheduleService.config";
try
{
ResourceScheduleConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
catch
{
}
}

/// <summary>
/// 文件改变:重新加载配置
/// </summary>
/// <param name="source"> 事件源 object</param>
/// <param name="e"> 事件参数 FileSystemEventArgs</param>
private static void OnChanged(object source, FileSystemEventArgs e)
{
InitConnectionConfig();
RaiseEvent();
}
}

标签:

评论已关闭!

Copyright © 极品飞鸽 保留所有权利.   Theme  Ality 蜀ICP备14015766号-1

用户登录

分享到: