diff --git a/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj b/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj index a8f436a..3a2a693 100644 --- a/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj +++ b/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj @@ -81,7 +81,7 @@ - {6F3878B0-1E07-4DE7-BFEC-509FF4443B71} + {6f3878b0-1e07-4de7-bfec-509ff4443b71} ConfigMonitor diff --git a/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.cs b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.cs index 4516fc4..ec6b85e 100644 --- a/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.cs +++ b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.cs @@ -19,7 +19,7 @@ namespace ConfigMonitor } _lastChange = DateTime.MinValue; - //var configFile = string.Concat(System.Reflection.Assembly.GetEntryAssembly().Location, ".config"); + configFilePath = string.Concat(System.Reflection.Assembly.GetEntryAssembly().Location, ".config"); if (File.Exists(configFilePath)) { _watcher = new FileSystemWatcher(Path.GetDirectoryName(configFilePath), Path.GetFileName(configFilePath)) @@ -34,7 +34,7 @@ namespace ConfigMonitor { _watcher.EnableRaisingEvents = false; _watcher.Changed -= Watcher_Changed; - _watcher.Dispose(); + _watcher.Dispose(); } public void Stop() @@ -49,7 +49,7 @@ namespace ConfigMonitor if ((DateTime.Now - _lastChange).Seconds <= 5 || IsFileLocked(e.FullPath)) return; var monitoredSections = ConfigurationManager.AppSettings["monitoredSections"]; - if (monitoredSections!= null) + if (monitoredSections != null) { IEnumerable sections = monitoredSections.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (sections.Any()) @@ -58,7 +58,7 @@ namespace ConfigMonitor { ConfigurationManager.RefreshSection(section); } - } + } } else { diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/ConfigMonitor.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/ConfigMonitor.cs new file mode 100644 index 0000000..026edea --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/ConfigMonitor.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WindowsDataCenter +{ + public class ConfigMonitor + { + private readonly FileSystemWatcher _watcher; + private DateTime _lastChange; + + public ConfigMonitor(string configFilePath) + { + if (configFilePath == null) + { + throw new ArgumentNullException(nameof(configFilePath)); + } + + _lastChange = DateTime.MinValue; + configFilePath = string.Concat(System.Reflection.Assembly.GetEntryAssembly().Location, ".config"); + if (File.Exists(configFilePath)) + { + _watcher = new FileSystemWatcher(Path.GetDirectoryName(configFilePath), Path.GetFileName(configFilePath)) + { + EnableRaisingEvents = true + }; + _watcher.Changed += Watcher_Changed; + } + } + + ~ConfigMonitor() + { + _watcher.EnableRaisingEvents = false; + _watcher.Changed -= Watcher_Changed; + _watcher.Dispose(); + } + + public void Stop() + { + _watcher.EnableRaisingEvents = false; + _watcher.Changed -= Watcher_Changed; + _watcher.Dispose(); + } + + private void Watcher_Changed(object sender, FileSystemEventArgs e) + { + if ((DateTime.Now - _lastChange).Seconds <= 5 || IsFileLocked(e.FullPath)) return; + var monitoredSections = ConfigurationManager.AppSettings["monitoredSections"]; + + if (monitoredSections != null) + { + IEnumerable sections = monitoredSections.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + if (sections.Any()) + { + foreach (var section in sections) + { + ConfigurationManager.RefreshSection(section); + } + } + } + else + { + Debug.WriteLine(ConfigurationManager.AppSettings["SwipeTimeGap"]); + ConfigurationManager.RefreshSection("AppSettings"); + Debug.WriteLine(ConfigurationManager.AppSettings["SwipeTimeGap"]); + } + _lastChange = DateTime.Now; + } + + private bool IsFileLocked(string filePath) + { + FileStream stream = null; + try + { + stream = File.OpenRead(filePath); + } + catch (IOException) + { + return true; + } + finally + { + stream?.Close(); + } + return false; + } + } +} diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs index 0fade9e..230fec1 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs @@ -18,7 +18,7 @@ namespace WindowsDataCenter private bool _stopMainWorkerThread; private Thread _mainWorkerThread; private ILogger _logger; - private ConfigMonitor.ConfigMonitor _cfgWatcher; + private ConfigMonitor _cfgWatcher; public void Start() { @@ -38,7 +38,7 @@ namespace WindowsDataCenter _logger = NinjectHelper.GetInstance().Get(); _logger.Trace("Starting Data Center Service"); - _cfgWatcher = new ConfigMonitor.ConfigMonitor(configPath); + _cfgWatcher = new ConfigMonitor(configPath); _logger.Trace("Monitoring App.config for changes"); _mainWorkerThread = new Thread(MainWorkerThread) { diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj index 2f69bcc..65efa83 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj @@ -147,6 +147,7 @@ + @@ -212,7 +213,9 @@ Always - + + Designer + PreserveNewest @@ -259,10 +262,6 @@ - - {6F3878B0-1E07-4DE7-BFEC-509FF4443B71} - ConfigMonitor - {B7347B72-E208-423A-9D99-723B558EA3D7} Interfaces