diff --git a/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.cs b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.cs new file mode 100644 index 0000000..4516fc4 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Linq; + +namespace ConfigMonitor +{ + 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; + //var configFile = 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 + { + ConfigurationManager.RefreshSection("appSettings"); + } + _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/ConfigMonitor/ConfigMonitor.csproj b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.csproj new file mode 100644 index 0000000..6e23f2e --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/ConfigMonitor.csproj @@ -0,0 +1,55 @@ + + + + + Debug + AnyCPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71} + Library + Properties + ConfigMonitor + ConfigMonitor + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/Properties/AssemblyInfo.cs b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..380067c --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/ConfigMonitor/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ConfigMonitor")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ConfigMonitor")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("6f3878b0-1e07-4de7-bfec-509ff4443b71")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter.sln b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter.sln index 6bfe705..d70d39a 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter.sln +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter.sln @@ -28,6 +28,8 @@ Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "CardReaderServiceInstaller" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Installers", "Installers", "{10A7E78C-0D11-40DD-AEC3-27C2C507A926}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigMonitor", "ConfigMonitor\ConfigMonitor.csproj", "{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -104,6 +106,14 @@ Global {119216DE-FC7F-408A-9C2C-105874449E42}.Release|Any CPU.ActiveCfg = Release|x86 {119216DE-FC7F-408A-9C2C-105874449E42}.Release|x86.ActiveCfg = Release|x86 {119216DE-FC7F-408A-9C2C-105874449E42}.Release|x86.Build.0 = Release|x86 + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|x86.ActiveCfg = Debug|Any CPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|x86.Build.0 = Debug|Any CPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|Any CPU.Build.0 = Release|Any CPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|x86.ActiveCfg = Release|Any CPU + {6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE