Files
DP44/Common/DTS.Common.Utilities/ConfigInitializationHelper.cs
2026-04-17 14:55:32 -04:00

77 lines
3.2 KiB
C#

using System.Diagnostics;
using System;
using System.IO;
using System.Configuration;
using DTS.Common.Utilities.Properties;
using DTS.Common.Utilities.Logging;
namespace DTS.Common.Utilities
{
public static class ConfigInitializationHelper
{
public static void UpdateTSRAIRAppSettings(string targetDir, bool launchTSRAIRGoValue)
{
try
{
APILogger.Log($"Calling GetNewSettings with targetDir={targetDir}");
if (!GetNewSettings(Resources.ApplicationSettings, targetDir, out var result, out var settings, out var config))
{
APILogger.Log($"failed to updatetsrairappsetting {result}");
}
APILogger.Log($"Calling settings.Get");
var setting = settings.Get("LaunchTSRAIRGo");
settings.Remove(setting);
setting.Value.ValueXml.InnerXml = launchTSRAIRGoValue ? "True" : "False";
settings.Add(setting);
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception ex)
{
APILogger.Log($"Failed to update tsr air app settings {ex.Message}");
//nothing to do right now
}
}
public static bool GetNewSettings(string settingsType, string targetDir,
out string result, out SettingElementCollection newSettings, out Configuration newConfig)
{
result = string.Empty;
newSettings = null;
newConfig = null;
var newPath = string.Empty;
try
{
//Open the new config file just installed
newPath = Path.Combine(targetDir, Resources.RegistryDataPROExe);
newConfig = ConfigurationManager.OpenExeConfiguration(@newPath);
}
catch (Exception ex)
{
APILogger.Log($"In catch: {ex.Message}");
result = string.Format(Resources.NewSettingsCouldNotBeFound, ex.Message, newPath);
return false;
}
APILogger.Log($"newPath & newConfig found; calling GetConfigSettings with {settingsType}");
newSettings = GetConfigSettings(settingsType, newConfig);
APILogger.Log($"back from GetConfigSettings; newSettings = {newSettings}");
if (newSettings != null) return true;
result = Resources.NewSettingsCouldNotBeProcessed;
return false;
}
private static SettingElementCollection GetConfigSettings(string settingsType, Configuration config)
{
var clientSettingsSection = new ClientSettingsSection();
var configurationSectionGroup = config.SectionGroups[settingsType];
if ((configurationSectionGroup != null) && (configurationSectionGroup.Sections[0] != null))
{
clientSettingsSection = configurationSectionGroup.Sections[0] as ClientSettingsSection;
}
return clientSettingsSection?.Settings;
}
}
}