From 23aedbc47af1014212c8a4ea01a4b03132da3b42 Mon Sep 17 00:00:00 2001 From: hzhuangxin01 Date: Thu, 21 Feb 2019 11:25:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E6=9F=A5=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=98=AF=E5=90=A6=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E4=BF=AE=E6=94=B9=E5=90=8E=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GeneratorCode/Configure/NConfig.cs | 105 +++++++++++++++-------------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/GeneratorCode/Configure/NConfig.cs b/GeneratorCode/Configure/NConfig.cs index d9174e8..b1a5e87 100644 --- a/GeneratorCode/Configure/NConfig.cs +++ b/GeneratorCode/Configure/NConfig.cs @@ -11,77 +11,73 @@ // // // *********************************************************************** + +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 { /// - /// Class NConfig. + /// Class NConfig. /// public static class NConfig { /// - /// Delegate ConfigChangedHandle - /// - public delegate void ConfigChangedHandle(); - - /// - /// The CFG file name + /// The CFG file name /// private static readonly string _cfgFileName = @".\config.ini"; /// - /// The CFG data + /// The CFG data /// private static IniData _cfgData; /// - /// Occurs when [on configuration changed]. + /// Delegate ConfigChangedHandle + /// + public delegate void ConfigChangedHandle(); + + /// + /// Occurs when [on configuration changed]. /// public static event ConfigChangedHandle OnConfigChanged; /// - /// Configurations the changed. + /// Gets the CFG value. /// - private static void ConfigChanged() + /// + /// Name of the sec. + /// Name of the key. + /// The definition value. + /// T. + public static T GetCfgValue([NotNull] string secName, [NotNull] string keyName, T defValue = default(T)) { - OnConfigChanged?.Invoke(); - } + var ret = defValue; + + if (_cfgData.Equals(null)) return ret; - /// - /// 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); + 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; } /// - /// Initializes the configure. + /// Initializes the configure. /// 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 } /// - /// Gets the CFG value. + /// Configurations the changed. /// - /// - /// Name of the sec. - /// Name of the key. - /// The definition value. - /// T. - public static T GetCfgValue([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(); + } + /// + /// Loads the CFG from file. + /// + 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; } } -} +} \ No newline at end of file