2019-02-18 07:56:36 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public static class NConfig
|
|
|
|
|
{
|
2019-02-19 04:02:14 +00:00
|
|
|
|
public delegate void ConfigChangedHandle();
|
2019-02-18 07:56:36 +00:00
|
|
|
|
|
2019-02-19 04:02:14 +00:00
|
|
|
|
private static readonly string _cfgFileName = @".\config.ini";
|
2019-02-18 07:56:36 +00:00
|
|
|
|
|
2019-02-19 04:02:14 +00:00
|
|
|
|
private static IniData _cfgData;
|
2019-02-18 07:56:36 +00:00
|
|
|
|
|
|
|
|
|
public static event ConfigChangedHandle OnConfigChanged;
|
|
|
|
|
|
|
|
|
|
private static void ConfigChanged()
|
|
|
|
|
{
|
|
|
|
|
OnConfigChanged?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 04:02:14 +00:00
|
|
|
|
var iniPraser = new FileIniDataParser();
|
2019-02-18 07:56:36 +00:00
|
|
|
|
iniPraser.Parser.Configuration.CommentString = ";";
|
|
|
|
|
_cfgData = iniPraser.ReadFile(_cfgFileName);
|
|
|
|
|
//Trace.WriteLine(_cfgData);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Trace.WriteLine(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void InitConfigure()
|
|
|
|
|
{
|
|
|
|
|
var cfgTask = new List<string>();
|
|
|
|
|
var appPath = Process.GetCurrentProcess().MainModule.FileName;
|
2019-02-19 04:02:14 +00:00
|
|
|
|
|
2019-02-18 11:21:24 +00:00
|
|
|
|
//Trace.WriteLine("Application: " + appPath);
|
|
|
|
|
//Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath));
|
|
|
|
|
//Trace.WriteLine("File: " + Path.GetFileName(appPath));
|
2019-02-18 07:56:36 +00:00
|
|
|
|
|
2019-02-19 04:02:14 +00:00
|
|
|
|
var fw = new FileSystemWatcher
|
2019-02-18 07:56:36 +00:00
|
|
|
|
{
|
|
|
|
|
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
|
2019-02-18 11:21:24 +00:00
|
|
|
|
Filter = _cfgFileName,
|
|
|
|
|
Path = Path.GetDirectoryName(appPath),
|
2019-02-19 04:02:14 +00:00
|
|
|
|
IncludeSubdirectories = false
|
2019-02-18 07:56:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T GetCfgValue<T>([NotNull] string secName, [NotNull] string keyName, T defValue = default(T))
|
|
|
|
|
{
|
|
|
|
|
var ret = defValue;
|
|
|
|
|
|
2019-02-19 04:02:14 +00:00
|
|
|
|
if (_cfgData.Equals(null)) return ret;
|
2019-02-18 07:56:36 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ret = (T) Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
|
|
|
|
|
}
|
2019-02-19 04:02:14 +00:00
|
|
|
|
catch (Exception e)
|
2019-02-18 07:56:36 +00:00
|
|
|
|
{
|
|
|
|
|
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
|
|
|
|
|
ret = defValue;
|
2019-02-19 04:02:14 +00:00
|
|
|
|
throw e;
|
2019-02-18 07:56:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|