检查配置文件是否修改,如果修改后自动重新加载配置

This commit is contained in:
hzhuangxin01 2019-02-21 11:25:30 +08:00
parent 59defae499
commit 23aedbc47a
1 changed files with 53 additions and 52 deletions

View File

@ -11,77 +11,73 @@
// </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
{
/// <summary>
/// Class NConfig.
/// Class NConfig.
/// </summary>
public static class NConfig
{
/// <summary>
/// Delegate ConfigChangedHandle
/// </summary>
public delegate void ConfigChangedHandle();
/// <summary>
/// The CFG file name
/// The CFG file name
/// </summary>
private static readonly string _cfgFileName = @".\config.ini";
/// <summary>
/// The CFG data
/// The CFG data
/// </summary>
private static IniData _cfgData;
/// <summary>
/// Occurs when [on configuration changed].
/// Delegate ConfigChangedHandle
/// </summary>
public delegate void ConfigChangedHandle();
/// <summary>
/// Occurs when [on configuration changed].
/// </summary>
public static event ConfigChangedHandle OnConfigChanged;
/// <summary>
/// Configurations the changed.
/// Gets the CFG value.
/// </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
{
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);
ret = (T)Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
}
catch (Exception e)
{
Trace.WriteLine(e.Message);
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
ret = defValue;
throw e;
}
return ret;
}
/// <summary>
/// Initializes the configure.
/// Initializes the configure.
/// </summary>
public static void InitConfigure()
{
@ -102,7 +98,7 @@ namespace GeneratorCode.Configure
fw.Changed += (sender, e) =>
{
//if (Path.GetFileName(appPath) != _cfgFileName) return;
if (Path.GetFileName(appPath) != _cfgFileName) return;
lock (cfgTask)
{
@ -133,31 +129,36 @@ namespace GeneratorCode.Configure
}
/// <summary>
/// Gets the CFG value.
/// Configurations the changed.
/// </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))
private static void ConfigChanged()
{
var ret = defValue;
if (_cfgData.Equals(null)) return ret;
OnConfigChanged?.Invoke();
}
/// <summary>
/// Loads the CFG from file.
/// </summary>
private static void LoadCfgFromFile()
{
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)
{
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
ret = defValue;
throw e;
Trace.WriteLine(e.Message);
}
return ret;
}
}
}