Files
BeWoPlaner/Shared/Core/AppSettingReader.cs

97 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BS.Shared.Core
{
internal struct Module
{
public string Short { get; set; }
public string Name { get; set; }
public Module(string shortName, string name)
{
Short = shortName;
Name = name;
}
};
internal struct SmtpConnectionInfo
{
public string SmtpServer { get; set; }
public bool Enabled { get; set; }
public string SmtpUser { get; set; }
public string SmtpPassword { get; set; }
public string MailSender { get; set; }
public string MailReceiver { get; set; }
}
public static class AppSettingReader
{
private static readonly string _AppSettingsSmtpServer = "SendMailModuleSmtpServer";
private static readonly string _AppSettingsEnabledFormat = "SendMailModule{0}Enabled";
private static readonly string _AppSettingsSmtpUserFormat = "SendMailModule{0}SmtpUser";
private static readonly string _AppSettingsSmtpPasswordFormat = "SendMailModule{0}SmtpPassword";
private static readonly string _AppSettingsSenderFormat = "SendMailModule{0}Sender";
private static readonly string _AppSettingsReceiverFormat = "SendMailModule{0}Receiver";
#region SmtpConnectionInfo
internal static SmtpConnectionInfo GetSmtpConnectionInfo(Module module)
=> GetSmtpConnectionInfo(module.Short);
internal static SmtpConnectionInfo GetSmtpConnectionInfo(string key)
{
return new SmtpConnectionInfo()
{
SmtpServer = GetSmtpServer(),
Enabled = GetSendMailModuleEnabled(key),
SmtpUser = GetSendMailModuleUser(key),
SmtpPassword = GetSendMailModulePassword(key),
MailSender = GetSendMailModuleSender(key),
MailReceiver = GetSendMailModuleReceiver(key)
};
}
private static string GetSmtpServer()
{
var setting_path = _AppSettingsSmtpServer;
var setting = ConfigurationManager.AppSettings.Get(setting_path);
return setting;
}
private static bool GetSendMailModuleEnabled(string key)
{
var setting_path = string.Format(_AppSettingsEnabledFormat, key);
var setting = ConfigurationManager.AppSettings.Get(setting_path);
if (bool.TryParse(setting, out var rtn))
return rtn;
return true;
}
private static string GetSendMailModuleUser(string key)
{
var setting_path = string.Format(_AppSettingsSmtpUserFormat, key);
var setting = ConfigurationManager.AppSettings.Get(setting_path);
return setting;
}
private static string GetSendMailModulePassword(string key)
{
var setting_path = string.Format(_AppSettingsSmtpPasswordFormat, key);
var setting = ConfigurationManager.AppSettings.Get(setting_path);
return setting;
}
private static string GetSendMailModuleSender(string key)
{
var setting_path = string.Format(_AppSettingsSenderFormat, key);
var setting = ConfigurationManager.AppSettings.Get(setting_path);
return setting;
}
private static string GetSendMailModuleReceiver(string key)
{
var setting_path = string.Format(_AppSettingsReceiverFormat, key);
var setting = ConfigurationManager.AppSettings.Get(setting_path);
return setting;
}
#endregion
}
}