检查配置文件是否修改,如果修改后自动重新加载配置
This commit is contained in:
parent
59defae499
commit
23aedbc47a
|
@ -11,77 +11,73 @@
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
|
||||||
|
using IniParser;
|
||||||
|
using IniParser.Model;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Timers;
|
using System.Timers;
|
||||||
using IniParser;
|
|
||||||
using IniParser.Model;
|
|
||||||
using JetBrains.Annotations;
|
|
||||||
|
|
||||||
namespace GeneratorCode.Configure
|
namespace GeneratorCode.Configure
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class NConfig.
|
/// Class NConfig.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class NConfig
|
public static class NConfig
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delegate ConfigChangedHandle
|
/// The CFG file name
|
||||||
/// </summary>
|
|
||||||
public delegate void ConfigChangedHandle();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The CFG file name
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly string _cfgFileName = @".\config.ini";
|
private static readonly string _cfgFileName = @".\config.ini";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The CFG data
|
/// The CFG data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static IniData _cfgData;
|
private static IniData _cfgData;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when [on configuration changed].
|
/// Delegate ConfigChangedHandle
|
||||||
|
/// </summary>
|
||||||
|
public delegate void ConfigChangedHandle();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Occurs when [on configuration changed].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static event ConfigChangedHandle OnConfigChanged;
|
public static event ConfigChangedHandle OnConfigChanged;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Configurations the changed.
|
/// Gets the CFG value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static void ConfigChanged()
|
/// <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))
|
||||||
{
|
{
|
||||||
OnConfigChanged?.Invoke();
|
var ret = defValue;
|
||||||
}
|
|
||||||
|
if (_cfgData.Equals(null)) return ret;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Loads the CFG from file.
|
|
||||||
/// </summary>
|
|
||||||
private static void LoadCfgFromFile()
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!File.Exists(_cfgFileName))
|
ret = (T)Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
|
||||||
{
|
|
||||||
var fs = new FileStream(_cfgFileName, FileMode.Create, FileAccess.Write);
|
|
||||||
var ws = new StreamWriter(fs);
|
|
||||||
ws.WriteLine("; 应用程序配置文件\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
var iniPraser = new FileIniDataParser();
|
|
||||||
iniPraser.Parser.Configuration.CommentString = ";";
|
|
||||||
_cfgData = iniPraser.ReadFile(_cfgFileName);
|
|
||||||
//Trace.WriteLine(_cfgData);
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Trace.WriteLine(e.Message);
|
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
|
||||||
|
ret = defValue;
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the configure.
|
/// Initializes the configure.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void InitConfigure()
|
public static void InitConfigure()
|
||||||
{
|
{
|
||||||
|
@ -102,7 +98,7 @@ namespace GeneratorCode.Configure
|
||||||
|
|
||||||
fw.Changed += (sender, e) =>
|
fw.Changed += (sender, e) =>
|
||||||
{
|
{
|
||||||
//if (Path.GetFileName(appPath) != _cfgFileName) return;
|
if (Path.GetFileName(appPath) != _cfgFileName) return;
|
||||||
|
|
||||||
lock (cfgTask)
|
lock (cfgTask)
|
||||||
{
|
{
|
||||||
|
@ -133,31 +129,36 @@ namespace GeneratorCode.Configure
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the CFG value.
|
/// Configurations the changed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
private static void ConfigChanged()
|
||||||
/// <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;
|
OnConfigChanged?.Invoke();
|
||||||
|
}
|
||||||
if (_cfgData.Equals(null)) return ret;
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads the CFG from file.
|
||||||
|
/// </summary>
|
||||||
|
private static void LoadCfgFromFile()
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ret = (T) Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
|
if (!File.Exists(_cfgFileName))
|
||||||
|
{
|
||||||
|
var fs = new FileStream(_cfgFileName, FileMode.Create, FileAccess.Write);
|
||||||
|
var ws = new StreamWriter(fs);
|
||||||
|
ws.WriteLine("; 应用程序配置文件\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
var iniPraser = new FileIniDataParser();
|
||||||
|
iniPraser.Parser.Configuration.CommentString = ";";
|
||||||
|
_cfgData = iniPraser.ReadFile(_cfgFileName);
|
||||||
|
//Trace.WriteLine(_cfgData);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
|
Trace.WriteLine(e.Message);
|
||||||
ret = defValue;
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue