diff --git a/TFU002/TFU002.Interfaces/Services/IDirectoryProvider.cs b/TFU002/TFU002.Interfaces/Services/IDirectoryProvider.cs index 77fa152..8620def 100644 --- a/TFU002/TFU002.Interfaces/Services/IDirectoryProvider.cs +++ b/TFU002/TFU002.Interfaces/Services/IDirectoryProvider.cs @@ -3,5 +3,6 @@ public interface IDirectoryProvider { string AssemblyDirectory { get; } + string DefaultPath { get; } } } \ No newline at end of file diff --git a/TFU002/TFU002.Logic/Services/DirectoryProvider.cs b/TFU002/TFU002.Logic/Services/DirectoryProvider.cs index e824b2c..1b7149f 100644 --- a/TFU002/TFU002.Logic/Services/DirectoryProvider.cs +++ b/TFU002/TFU002.Logic/Services/DirectoryProvider.cs @@ -21,5 +21,6 @@ public DirectoryProvider(ILogger logger) } public string AssemblyDirectory { get; } + public string DefaultPath => @"C:\TwinCAT\Functions\Unofficial\BeckhoffS7Client"; } } \ No newline at end of file diff --git a/TFU002/TFU002.Logic/Services/SettingsProvider.cs b/TFU002/TFU002.Logic/Services/SettingsProvider.cs index f761ea5..edf6a4e 100644 --- a/TFU002/TFU002.Logic/Services/SettingsProvider.cs +++ b/TFU002/TFU002.Logic/Services/SettingsProvider.cs @@ -17,7 +17,7 @@ public class SettingsProvider : ISettingsProvider public ApplicationSettings Settings { get; private set; } private void LoadOrCreateSettings() { - var settingsPath = Path.Combine(directoryProvider.AssemblyDirectory, "TFU002.settings.json"); + var settingsPath = Path.Combine(directoryProvider.DefaultPath, "TFU002.settings.json"); logger.LogInformation($"Looking for application settings into {settingsPath}"); try { @@ -28,11 +28,26 @@ private void LoadOrCreateSettings() } else { - logger.LogInformation("Application settings does'n not exist: creating new default settings"); - Settings = new ApplicationSettings(); - Settings.ExtenalPlcSettings.Add(new ExtenalPlcSetting()); - var json = JsonConvert.SerializeObject(Settings, Formatting.Indented); - File.WriteAllText(settingsPath, json); + logger.LogInformation("Application settings do not exist..."); + var fallbackSettingsPath = Path.Combine(directoryProvider.AssemblyDirectory, "TFU002.settings.json"); + logger.LogInformation($"looking for settings into {fallbackSettingsPath}..."); + if (File.Exists(fallbackSettingsPath)) + { + Settings = JsonConvert.DeserializeObject(File.ReadAllText(fallbackSettingsPath)); + logger.LogInformation("Application settings successfully loaded!"); + } + else + { + logger.LogInformation("Application settings not found also in fallback path..."); + Settings = new ApplicationSettings(); + Settings.ExtenalPlcSettings.Add(new ExtenalPlcSetting()); + var json = JsonConvert.SerializeObject(Settings, Formatting.Indented); + if (Directory.Exists(directoryProvider.DefaultPath)) + { + logger.LogInformation($"Writing default settings into {settingsPath}"); + File.WriteAllText(settingsPath, json); + } + } } } catch (Exception e)