检查配置文件是否修改,如果修改后自动重新加载配置
This commit is contained in:
parent
59defae499
commit
23aedbc47a
|
@ -11,14 +11,15 @@
|
|||
// </copyright>
|
||||
// <summary></summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using IniParser;
|
||||
using IniParser.Model;
|
||||
using JetBrains.Annotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Timers;
|
||||
using IniParser;
|
||||
using IniParser.Model;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace GeneratorCode.Configure
|
||||
{
|
||||
|
@ -27,11 +28,6 @@ namespace GeneratorCode.Configure
|
|||
/// </summary>
|
||||
public static class NConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// Delegate ConfigChangedHandle
|
||||
/// </summary>
|
||||
public delegate void ConfigChangedHandle();
|
||||
|
||||
/// <summary>
|
||||
/// The CFG file name
|
||||
/// </summary>
|
||||
|
@ -42,11 +38,96 @@ namespace GeneratorCode.Configure
|
|||
/// </summary>
|
||||
private static IniData _cfgData;
|
||||
|
||||
/// <summary>
|
||||
/// Delegate ConfigChangedHandle
|
||||
/// </summary>
|
||||
public delegate void ConfigChangedHandle();
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when [on configuration changed].
|
||||
/// </summary>
|
||||
public static event ConfigChangedHandle OnConfigChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the CFG value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="secName">Name of the sec.</param>
|
||||
/// <param name="keyName">Name of the key.</param>
|
||||
/// <param name="defValue">The definition value.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T GetCfgValue<T>([NotNull] string secName, [NotNull] string keyName, T defValue = default(T))
|
||||
{
|
||||
var ret = defValue;
|
||||
|
||||
if (_cfgData.Equals(null)) return ret;
|
||||
|
||||
try
|
||||
{
|
||||
ret = (T)Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
|
||||
ret = defValue;
|
||||
throw e;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the configure.
|
||||
/// </summary>
|
||||
public static void InitConfigure()
|
||||
{
|
||||
var cfgTask = new List<string>();
|
||||
var appPath = Process.GetCurrentProcess().MainModule.FileName;
|
||||
|
||||
//Trace.WriteLine("Application: " + appPath);
|
||||
//Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath));
|
||||
//Trace.WriteLine("File: " + Path.GetFileName(appPath));
|
||||
|
||||
var fw = new FileSystemWatcher
|
||||
{
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
|
||||
Filter = _cfgFileName,
|
||||
Path = Path.GetDirectoryName(appPath),
|
||||
IncludeSubdirectories = false
|
||||
};
|
||||
|
||||
fw.Changed += (sender, e) =>
|
||||
{
|
||||
if (Path.GetFileName(appPath) != _cfgFileName) return;
|
||||
|
||||
lock (cfgTask)
|
||||
{
|
||||
if (cfgTask.Contains(e.FullPath)) return;
|
||||
|
||||
cfgTask.Add(e.FullPath);
|
||||
}
|
||||
|
||||
var tm = new Timer(1000) { AutoReset = false };
|
||||
|
||||
tm.Elapsed += (obj, args) =>
|
||||
{
|
||||
lock (cfgTask)
|
||||
{
|
||||
cfgTask.Remove(e.FullPath);
|
||||
LoadCfgFromFile();
|
||||
ConfigChanged();
|
||||
// LogOut("File: " + e.FullPath + " ==> " + e.ChangeType.ToString() + "\n");
|
||||
}
|
||||
};
|
||||
|
||||
tm.Start();
|
||||
};
|
||||
|
||||
fw.EnableRaisingEvents = true;
|
||||
|
||||
LoadCfgFromFile();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configurations the changed.
|
||||
/// </summary>
|
||||
|
@ -79,85 +160,5 @@ namespace GeneratorCode.Configure
|
|||
Trace.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the configure.
|
||||
/// </summary>
|
||||
public static void InitConfigure()
|
||||
{
|
||||
var cfgTask = new List<string>();
|
||||
var appPath = Process.GetCurrentProcess().MainModule.FileName;
|
||||
|
||||
//Trace.WriteLine("Application: " + appPath);
|
||||
//Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath));
|
||||
//Trace.WriteLine("File: " + Path.GetFileName(appPath));
|
||||
|
||||
var fw = new FileSystemWatcher
|
||||
{
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
|
||||
Filter = _cfgFileName,
|
||||
Path = Path.GetDirectoryName(appPath),
|
||||
IncludeSubdirectories = false
|
||||
};
|
||||
|
||||
fw.Changed += (sender, e) =>
|
||||
{
|
||||
//if (Path.GetFileName(appPath) != _cfgFileName) return;
|
||||
|
||||
lock (cfgTask)
|
||||
{
|
||||
if (cfgTask.Contains(e.FullPath)) return;
|
||||
|
||||
cfgTask.Add(e.FullPath);
|
||||
}
|
||||
|
||||
var tm = new Timer(1000) { AutoReset = false };
|
||||
|
||||
tm.Elapsed += (obj, args) =>
|
||||
{
|
||||
lock (cfgTask)
|
||||
{
|
||||
cfgTask.Remove(e.FullPath);
|
||||
LoadCfgFromFile();
|
||||
ConfigChanged();
|
||||
// LogOut("File: " + e.FullPath + " ==> " + e.ChangeType.ToString() + "\n");
|
||||
}
|
||||
};
|
||||
|
||||
tm.Start();
|
||||
};
|
||||
|
||||
fw.EnableRaisingEvents = true;
|
||||
|
||||
LoadCfgFromFile();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the CFG value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="secName">Name of the sec.</param>
|
||||
/// <param name="keyName">Name of the key.</param>
|
||||
/// <param name="defValue">The definition value.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T GetCfgValue<T>([NotNull] string secName, [NotNull] string keyName, T defValue = default(T))
|
||||
{
|
||||
var ret = defValue;
|
||||
|
||||
if (_cfgData.Equals(null)) return ret;
|
||||
|
||||
try
|
||||
{
|
||||
ret = (T) Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
|
||||
ret = defValue;
|
||||
throw e;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue