171 lines
5.2 KiB
C#
171 lines
5.2 KiB
C#
// ***********************************************************************
|
|
// Assembly : GeneratorCode
|
|
// Author : 黄昕 <hzhuangxin01@corp.netease.com>
|
|
// Created : 02-15-2019
|
|
//
|
|
// Last Modified By : 黄昕 <hzhuangxin01@corp.netease.com>
|
|
// Last Modified On : 02-20-2019
|
|
// ***********************************************************************
|
|
// <copyright file="NConfig.cs" company="NetEase">
|
|
// Copyright © 2019
|
|
// </copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
|
|
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.
|
|
/// </summary>
|
|
public static class NConfig
|
|
{
|
|
/// <summary>
|
|
/// Delegate ConfigChangedHandle
|
|
/// </summary>
|
|
public delegate void ConfigChangedHandle();
|
|
|
|
/// <summary>
|
|
/// The CFG file name
|
|
/// </summary>
|
|
private static readonly string _cfgFileName = @".\config.ini";
|
|
|
|
/// <summary>
|
|
/// The CFG data
|
|
/// </summary>
|
|
private static IniData _cfgData;
|
|
|
|
/// <summary>
|
|
/// Occurs when [on configuration changed].
|
|
/// </summary>
|
|
public static event ConfigChangedHandle OnConfigChanged;
|
|
|
|
/// <summary>
|
|
/// Gets the CFG value.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="segName">Name of the segment.</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 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes the configure.
|
|
/// </summary>
|
|
public static void InitConfigure()
|
|
{
|
|
var cfgTask = new List<string>();
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configurations the changed.
|
|
/// </summary>
|
|
private static void ConfigChanged()
|
|
{
|
|
OnConfigChanged?.Invoke();
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Trace.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
} |