// *********************************************************************** // Assembly : GeneratorCode // Author : 黄昕 // Created : 02-15-2019 // // Last Modified By : 黄昕 // Last Modified On : 02-20-2019 // *********************************************************************** // // Copyright © 2019 // // // *********************************************************************** 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 { /// /// Class NConfig. /// public static class NConfig { /// /// Delegate ConfigChangedHandle /// public delegate void ConfigChangedHandle(); /// /// The CFG file name /// private static readonly string _cfgFileName = @".\config.ini"; /// /// The CFG data /// private static IniData _cfgData; /// /// Occurs when [on configuration changed]. /// public static event ConfigChangedHandle OnConfigChanged; /// /// Gets the CFG value. /// /// /// Name of the segment. /// Name of the key. /// The definition value. /// T. public static T GetCfgValue([NotNull] string segName, [NotNull] string keyName, T defValue = default(T)) { var ret = defValue; if (_cfgData.Equals(null)) return ret; try { string val; if (_cfgData.TryGetKey(segName + _cfgData.SectionKeySeparator + keyName, out val)) { ret = (T)Convert.ChangeType(val, typeof(T)); } } catch (Exception e) { Trace.WriteLine("[" + _cfgData[segName][keyName] + "] :" + e.Message); ret = defValue; } return ret; } /// /// Initializes the configure. /// public static void InitConfigure() { var cfgTask = new List(); 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(); } /// /// Configurations the changed. /// private static void ConfigChanged() { OnConfigChanged?.Invoke(); } /// /// Loads the CFG from file. /// 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); } catch (Exception e) { Trace.WriteLine(e.Message); } } } }