using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Watchdog.WatchdogLib.Registry;
using Watchdog.WatchdogLib.Assembly;
using Watchdog.WatchdogLib.Monitor;
using Watchdog.WatchdogLib.Process;
using System.Diagnostics;
using WatchdogLib.Tools;
namespace Watchdog
{
internal class Settings
{
///
/// Critical Section
///
private object _lock = new object();
#region Construction
///
/// Construction
///
internal Settings()
{
}
#endregion
#region Scheduler N' Scheduler Backup Settings
///
/// Minimum Minute Interval for Schedulers
///
public const uint MINIMUM_MINUTE_INTERVAL_SCHEDULERS = 10;
///
/// True if there are Settings specified for the Scheduler
///
internal bool SchedulerSettingsExist
{
get
{
DateTime dtStart = DateTime.MinValue;
DateTime dtLastRun = DateTime.MinValue;
uint nHoursOrMinutes = 0;
bool bIsHours = true;
return GetSchedulerSettings(out dtStart, out dtLastRun, out nHoursOrMinutes, out bIsHours);
}
}
///
/// True if there are Settings specified for the Scheduler Backup
///
internal bool SchedulerBackupSettingsExist
{
get
{
DateTime dtStart = DateTime.MinValue;
DateTime dtLastRun = DateTime.MinValue;
uint nHoursOrMinutes = 0;
bool bIsHours = true;
return GetSchedulerBackupSettings(out dtStart, out dtLastRun, out nHoursOrMinutes, out bIsHours);
}
}
///
/// Use this to retrieve the Scheduler Settings
///
/// Start Time
/// Last Run Time
/// number of Hours or Minutes
/// is hours
/// Returns true if there is Scheduler Information in the Settings, also returns the settings, false otherwise
internal bool GetSchedulerSettings(out DateTime dtStart, out DateTime dtLastRun, out uint nHoursOrMinutes, out bool bIsHours)
{
lock (_lock)
{
dtStart = DateTime.MinValue;
dtLastRun = DateTime.MinValue;
nHoursOrMinutes = 0;
bIsHours = true;
if (uint.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatEveryNHoursOrMinutes, "0"), out nHoursOrMinutes) &&
DateTime.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_StartDateTime, DateTime.MinValue.ToString()), out dtStart) &&
nHoursOrMinutes > 0 &&
dtStart != DateTime.MinValue
)
{
// Get the Last time this scheduler was run
dtLastRun = DateTime.MinValue;
DateTime.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_LastRun_DateTime, DateTime.MinValue.ToString()), out dtLastRun);
// Are we on an hourly or minute schedule?
bIsHours = App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatIsHour, true);
// Minimum Interval for Minutes
if (!bIsHours && nHoursOrMinutes < MINIMUM_MINUTE_INTERVAL_SCHEDULERS)
nHoursOrMinutes = MINIMUM_MINUTE_INTERVAL_SCHEDULERS;
// return only true if nHoursOrMinutes is bigger 0 and dtStart is NOT equal to MinVal
return true;
}
return false;
}
}
///
/// Use this to retrieve the Scheduler Backup Settings
///
/// Start Time
/// Last Run Time
/// number of Hours or Minutes
/// is hours
/// Returns true if there is Scheduler Backup Information in the Settings, also returns the settings, false otherwise
internal bool GetSchedulerBackupSettings(out DateTime dtStart, out DateTime dtLastRun, out uint nHoursOrMinutes, out bool bIsHours)
{
lock (_lock)
{
dtStart = DateTime.MinValue;
dtLastRun = DateTime.MinValue;
nHoursOrMinutes = 0;
bIsHours = true;
if (uint.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_ForDurationNHoursOrMinutes, "0"), out nHoursOrMinutes) &&
DateTime.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_StartDateTime, DateTime.MinValue.ToString()), out dtStart) &&
nHoursOrMinutes > 0 &&
dtStart != DateTime.MinValue
)
{
// Get the Last time this scheduler was run
dtLastRun = DateTime.MinValue;
DateTime.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_LastRun_DateTime, DateTime.MinValue.ToString()), out dtLastRun);
// Are we on an hourly or minute schedule?
bIsHours = App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_DurationIsHour, true);
// Minimum Interval for Minutes
if (!bIsHours && nHoursOrMinutes < MINIMUM_MINUTE_INTERVAL_SCHEDULERS)
nHoursOrMinutes = MINIMUM_MINUTE_INTERVAL_SCHEDULERS;
// return only true if nHoursOrMinutes is bigger 0 and dtStart is NOT equal to MinVal
return true;
}
return false;
}
}
///
/// Set/Get the Scheduler Start DT
///
internal DateTime SchedulerStartDateTime
{
get
{
string strDateTime = App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_StartDateTime, DateTime.MinValue.ToString());
DateTime dt;
if (DateTime.TryParse(strDateTime, out dt) && dt != DateTime.MinValue)
return dt;
else
return DateTime.Now;
}
set
{
if (value != DateTime.MinValue)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_StartDateTime, value.ToString());
}
}
///
/// Set/Get the Scheduler Backup Start DT
///
internal DateTime SchedulerBackupStartDateTime
{
get
{
string strDateTime = App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_StartDateTime, DateTime.MinValue.ToString());
DateTime dt;
if (DateTime.TryParse(strDateTime, out dt) && dt != DateTime.MinValue)
return dt;
else
return DateTime.Now;
}
set
{
if (value != DateTime.MinValue)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_StartDateTime, value.ToString());
}
}
///
/// Set/Get the Scheduler Repeat Hours or Minutes
///
internal uint SchedulerRepeatEveryHoursOrMinutes
{
get
{
uint nRepeatEveryNHoursOrMinutes = 0;
if (uint.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatEveryNHoursOrMinutes, "0"), out nRepeatEveryNHoursOrMinutes) && (nRepeatEveryNHoursOrMinutes >= 1 && nRepeatEveryNHoursOrMinutes <= 8760))
{
if ((nRepeatEveryNHoursOrMinutes > 0) && (nRepeatEveryNHoursOrMinutes < MINIMUM_MINUTE_INTERVAL_SCHEDULERS) && !SchedulerRepeatIsHour)
return MINIMUM_MINUTE_INTERVAL_SCHEDULERS;
else
return nRepeatEveryNHoursOrMinutes;
}
else
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatEveryNHoursOrMinutes, "0");
return 0;
}
}
set
{
if (value >= 0 && value <= 8760)
{
if ((value > 0) && (value < MINIMUM_MINUTE_INTERVAL_SCHEDULERS) && !SchedulerRepeatIsHour)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatEveryNHoursOrMinutes, MINIMUM_MINUTE_INTERVAL_SCHEDULERS.ToString());
else
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatEveryNHoursOrMinutes, value.ToString());
}
}
}
///
/// Set/Get the Scheduler Backup Duration Hours or Minutes
///
internal uint SchedulerBackupForDurationHoursOrMinutes
{
get
{
uint nForDurationNHoursOrMinutes = 0;
if (uint.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_ForDurationNHoursOrMinutes, "0"), out nForDurationNHoursOrMinutes) && (nForDurationNHoursOrMinutes >= 1 && nForDurationNHoursOrMinutes <= 8760))
{
if ((nForDurationNHoursOrMinutes > 0) && (nForDurationNHoursOrMinutes < MINIMUM_MINUTE_INTERVAL_SCHEDULERS) && !SchedulerBackupDurationIsHour)
return MINIMUM_MINUTE_INTERVAL_SCHEDULERS;
else
return nForDurationNHoursOrMinutes;
}
else
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_ForDurationNHoursOrMinutes, "0");
return 0;
}
}
set
{
if (value >= 0 && value <= 8760)
{
if ((value > 0) && (value < MINIMUM_MINUTE_INTERVAL_SCHEDULERS) && !SchedulerBackupDurationIsHour)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_ForDurationNHoursOrMinutes, MINIMUM_MINUTE_INTERVAL_SCHEDULERS.ToString());
else
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_ForDurationNHoursOrMinutes, value.ToString());
}
}
}
///
/// Set/Get the Scheduler Repeat Is Hour Flag
///
internal bool SchedulerRepeatIsHour
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatIsHour, true);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_RepeatIsHour, value);
}
}
///
/// Set/Get the Scheduler Backup Duration Is Hour Flag
///
internal bool SchedulerBackupDurationIsHour
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_DurationIsHour, true);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_DurationIsHour, value);
}
}
///
/// Boolean Flag indicating whether the Scheduler Restarts Services
///
internal bool SchedulerRestartsServices
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Scheduler_Restarts_Services, false);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Scheduler_Restarts_Services, value);
}
}
///
/// DT stamp when the Scheduler was last run
///
internal DateTime LastSchedulerRun
{
get
{
// Get the Last time this scheduler was run
DateTime dtLastRun = DateTime.MinValue;
DateTime.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_LastRun_DateTime, DateTime.MinValue.ToString()), out dtLastRun);
return dtLastRun;
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Sheduler_LastRun_DateTime, value.ToString());
}
}
///
/// DT stamp when the Scheduler Backup was last run
///
internal DateTime LastSchedulerBackupRun
{
get
{
// Get the Last time this scheduler was run
DateTime dtLastRun = DateTime.MinValue;
DateTime.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_LastRun_DateTime, DateTime.MinValue.ToString()), out dtLastRun);
return dtLastRun;
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_LastRun_DateTime, value.ToString());
}
}
///
/// Boolean Flag indicating whether the Scheduler Backup Stops Instead of Pauses
///
internal bool SchedulerBackupStopInsteadOfPause
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_StopInsteadOfPause, true);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_StopInsteadOfPause, value);
}
}
///
/// Boolean Flag indicating whether the Scheduler Backup Stops Servies
///
internal bool SchedulerBackupStopServices
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_StopServices, true);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__ShedulerBackup_StopServices, value);
}
}
#endregion
#region Program Settings
///
/// Boolean flag indicates whether upon program start it should start monitoring right away
///
internal bool StartMonitorUponStart
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Monitor_On_Start, true);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Monitor_On_Start, value);
}
}
///
/// Indicates the max failure count that can occur per process/service within a given MaxFailureCountHour Timespan
/// * Important Setting for Process Monitor * Value can be between 1 and 99
///
internal uint MaxFailureCount
{
get
{
// Re-read MaxFailureCount Setting * Default is specified in AppResx *
uint nMaxFailureCount = uint.Parse(AppResx.GetString("DEFAULT_VALUE_MAX_PROCESS_START_FAILURE_COUNT"));
bool bCanParseIni = uint.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Max_Fail_Count, AppResx.GetString("DEFAULT_VALUE_MAX_PROCESS_START_FAILURE_COUNT")), out nMaxFailureCount);
if (!bCanParseIni)
App.log.Error(AppResx.GetStringFormated("ERROR_INI_SETTINGS_PARSE", App.Ini_Setting.MonitorSettings__Max_Fail_Count.ToString()));
if (nMaxFailureCount < 1)
nMaxFailureCount = 1;
else if (nMaxFailureCount > 99)
nMaxFailureCount = 99;
return nMaxFailureCount;
}
set
{
if (value >= 1 && value <= 99)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Max_Fail_Count, value.ToString());
}
}
///
/// Indicates the Hour Timespan in which MaxFailureCount can occur
/// * Important Setting for Process Monitor * Value can be between 1 and 8760
///
internal uint MaxFailureCountHour
{
get
{
// Re-read MaxFailureCountHour Setting * Default is specified in AppResx
uint nMaxFailureCountHour = uint.Parse(AppResx.GetString("DEFAULT_VALUE_MAX_PROCESS_START_FAILURE_COUNT_HOUR"));
bool bCanParseIni = uint.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Max_Fail_Count_Per_Hour, AppResx.GetString("DEFAULT_VALUE_MAX_PROCESS_START_FAILURE_COUNT_HOUR")), out nMaxFailureCountHour);
if (!bCanParseIni)
App.log.Error(AppResx.GetStringFormated("ERROR_INI_SETTINGS_PARSE", App.Ini_Setting.MonitorSettings__Max_Fail_Count_Per_Hour.ToString()));
if (nMaxFailureCountHour < 1)
nMaxFailureCountHour = 1;
else if (nMaxFailureCountHour > 8760)
nMaxFailureCountHour = 8760;
return nMaxFailureCountHour;
}
set
{
if (value >= 1 && value <= 8760)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Max_Fail_Count_Per_Hour, value.ToString());
}
}
///
/// Get/Set the Last Program Version in the Registry * Used to check if an upgrade occured *
///
internal string LastProgramVersion
{
get
{
string LastVersion = RegKey.GetKey(App.APPLICATION_NAME_SHORT, "LastProgramVersion", String.Empty);
return LastVersion;
}
set
{
RegKey.SetKey(App.APPLICATION_NAME_SHORT, "LastProgramVersion", value);
}
}
#endregion
#region Email Settings
///
/// Boolean flag indicates whether Email Notifications are enabled
///
internal bool EmailNotificationEnabled
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__EnableEmailNotifications, false);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__EnableEmailNotifications, value);
}
}
///
/// True if the settings for Email are valid
///
internal bool EmailSettingsValid
{
get
{
// Check reg
bool NotValid = String.IsNullOrEmpty(EmailStmpServer) || String.IsNullOrEmpty(EmailSenderEmail) ||
String.IsNullOrEmpty(EmailReceiverEmail);
if (NotValid)
return false;
// Check auth
if (EmailStmpServerRequiresAuth)
NotValid = String.IsNullOrEmpty(EmailSmtpUsername) || String.IsNullOrEmpty(EmailSmtpPassword);
return !NotValid;
}
}
///
/// Boolean flag indicates whether Email Smtp Server Requires Authentication
///
internal bool EmailStmpServerRequiresAuth
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__SmtpRequiresAuth, false);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpRequiresAuth, value);
}
}
///
/// Boolean flag indicates whether Email Smtp Server Requires SSL
///
internal bool EmailStmpServerRequiresSSL
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__SmtpRequiresSSL, false);
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpRequiresSSL, value);
}
}
///
/// Email Smtp Server
///
internal string EmailStmpServer
{
get
{
string strFound = App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__SmtpServer, "");
if (!String.IsNullOrEmpty(strFound) && strFound.Contains('.'))
return strFound;
else
return String.Empty;
}
set
{
if(!String.IsNullOrEmpty(value) && value.Contains('.'))
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpServer, value);
}
}
///
/// Email Sender Email
///
internal string EmailSenderEmail
{
get
{
string strFound = App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__SenderEmail, "");
if (!String.IsNullOrEmpty(strFound) && strFound.Contains('.') && strFound.Contains('@'))
return strFound;
else
return String.Empty;
}
set
{
if (!String.IsNullOrEmpty(value) && value.Contains('.') && value.Contains('@'))
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SenderEmail, value);
}
}
///
/// Email Receiver Email
///
internal string EmailReceiverEmail
{
get
{
string strFound = App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__ReveiverEmail, "");
if (!String.IsNullOrEmpty(strFound) && strFound.Contains('.') && strFound.Contains('@'))
return strFound;
else
return String.Empty;
}
set
{
if (!String.IsNullOrEmpty(value) && value.Contains('.') && value.Contains('@'))
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__ReveiverEmail, value);
}
}
///
/// Email Smtp Port * Default is 25 *
///
internal uint EmailSmtpPort
{
get
{
uint nPort = 25;
bool bCanParseIni = uint.TryParse(App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__SmtpPort, "25"), out nPort);
if (!bCanParseIni)
App.log.Error(AppResx.GetStringFormated("ERROR_INI_SETTINGS_PARSE", App.Ini_Setting.EmailSettings__SmtpPort.ToString()));
if (nPort < 1)
nPort = 1;
else if (nPort > 65536)
nPort = 65536;
return nPort;
}
set
{
if (value >= 1 && value <= 65536)
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpPort, value.ToString());
}
}
///
/// Email Smtp Username
///
internal string EmailSmtpUsername
{
get
{
string strFound = App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__SmtpUsername, "");
if (!String.IsNullOrEmpty(strFound))
return strFound;
else
return String.Empty;
}
set
{
if (!String.IsNullOrEmpty(value))
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpUsername, value);
else
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpUsername, "");
}
}
///
/// Email Smtp Password
///
internal string EmailSmtpPassword
{
get
{
string strFound = App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__SmtpPassword, "");
if (!String.IsNullOrEmpty(strFound))
return strFound;
else
return String.Empty;
}
set
{
if (!String.IsNullOrEmpty(value))
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpPassword, value);
else
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__SmtpPassword, "");
}
}
///
/// Email Custom Message Text
///
internal string EmailCustomMessageText
{
get
{
string strFound = App.inifile.GetKeyValue(App.Ini_Setting.EmailSettings__Custom_Message_Text, "");
if (!String.IsNullOrEmpty(strFound))
return strFound;
else
return String.Empty;
}
set
{
if (!String.IsNullOrEmpty(value))
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__Custom_Message_Text, value);
else
App.inifile.SetKeyValue(App.Ini_Setting.EmailSettings__Custom_Message_Text, "");
}
}
#endregion
#region Windows Settings
///
/// Boolean Flag indicates whether this app should start with windows
///
internal bool StartWithWindows
{
get
{
bool bStartWithWindows = App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Start_With_Windows, true);
bool bSetOk = true;
if (bStartWithWindows)
bSetOk = RegKey.SetKey("Microsoft\\Windows\\CurrentVersion\\Run", App.APPLICATION_NAME_SHORT, AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameNPath(AssemblyW.AssemblyST.Entry));
else
bSetOk = RegKey.DeleteKey("Microsoft\\Windows\\CurrentVersion\\Run", App.APPLICATION_NAME_SHORT);
if (!bSetOk)
App.log.Error("Failed to write to Microsoft\\Windows\\CurrentVersion\\Run. Check Permissions.");
// * Special Circumstance *
if (bStartWithWindows)
DeleteOffendingHL7KeyIfApplicable();
return bStartWithWindows;
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Start_With_Windows, value);
bool bSetOk = true;
if (value)
bSetOk = RegKey.SetKey("Microsoft\\Windows\\CurrentVersion\\Run", App.APPLICATION_NAME_SHORT, AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameNPath(AssemblyW.AssemblyST.Entry));
else
bSetOk = RegKey.DeleteKey("Microsoft\\Windows\\CurrentVersion\\Run", App.APPLICATION_NAME_SHORT);
// * Special Circumstance *
if (value)
DeleteOffendingHL7KeyIfApplicable();
if (!bSetOk)
App.log.Error("Failed to write to HKCU\\Microsoft\\Windows\\CurrentVersion\\Run. Check Permissions.");
}
}
///
/// Boolean Flag indicates whether LockWorkstation should be called upon CU login
///
internal bool LockWorkstationWithWindows
{
get
{
bool bLockWorkstationWithWindows = App.inifile.GetKeyValue(App.Ini_Setting.WindowsSettings__LockWorkstation_With_Windows, false);
bool bSetOk = true;
if (bLockWorkstationWithWindows)
bSetOk = RegKey.SetKey("Microsoft\\Windows\\CurrentVersion\\Run", "ALockWorkstation", "rundll32.exe user32.dll LockWorkStation");
else
bSetOk = RegKey.DeleteKey("Microsoft\\Windows\\CurrentVersion\\Run", "ALockWorkstation");
if (!bSetOk)
App.log.Error("Failed to write to HKCU\\Microsoft\\Windows\\CurrentVersion\\Run. Check Permissions.");
return bLockWorkstationWithWindows;
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.WindowsSettings__LockWorkstation_With_Windows, value);
bool bSetOk = true;
if (value)
bSetOk = RegKey.SetKey("Microsoft\\Windows\\CurrentVersion\\Run", "ALockWorkstation", "rundll32.exe user32.dll LockWorkStation");
else
bSetOk = RegKey.DeleteKey("Microsoft\\Windows\\CurrentVersion\\Run", "ALockWorkstation");
if (!bSetOk)
App.log.Error("Failed to write to HKCU\\Microsoft\\Windows\\CurrentVersion\\Run. Check Permissions.");
}
}
///
/// Boolean Flag indicates whether Windows should automatically Login
///
internal bool EnableAutomaticLoginWithWindows
{
get
{
bool bAutoLogin = App.inifile.GetKeyValue(App.Ini_Setting.WindowsSettings__AutoLogin_With_Windows, false);
bool bSetOk = true;
if (bAutoLogin)
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "AutoAdminLogon", "1");
else
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "AutoAdminLogon", "0");
if (!bSetOk)
App.log.Error("Failed to write to HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon. Check Permissions.");
return bAutoLogin;
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.WindowsSettings__AutoLogin_With_Windows, value);
bool bSetOk = true;
if (value)
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "AutoAdminLogon", "1");
else
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "AutoAdminLogon", "0");
if (!bSetOk)
App.log.Error("Failed to write to HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon. Check Permissions.");
}
}
///
/// AutoLogin Default Domain
///
internal string AutomaticLoginDefaultDomain
{
get
{
return RegKey.GetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultDomainName", Environment.UserDomainName);
}
set
{
bool bSetOk = true;
if (!String.IsNullOrEmpty(value))
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultDomainName", value);
else
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultDomainName", "");
if (!bSetOk)
App.log.Error("Failed to write to HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon. Check Permissions.");
}
}
///
/// AutoLogin Default Username
///
internal string AutomaticLoginDefaultUsername
{
get
{
return RegKey.GetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultUserName", Environment.UserName);
}
set
{
bool bSetOk = true;
if (!String.IsNullOrEmpty(value))
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultUserName", value);
else
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultUserName", "");
if (!bSetOk)
App.log.Error("Failed to write to HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon. Check Permissions.");
}
}
///
/// AutoLogin Default Password
///
internal string AutomaticLoginDefaultPassword
{
get
{
return RegKey.GetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultPassword", "");
}
set
{
bool bSetOk = true;
if (!String.IsNullOrEmpty(value))
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultPassword", value);
else
bSetOk = RegKey.SetKey(HKEYRoot.LocalMachine, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultPassword", "");
if (!bSetOk)
App.log.Error("Failed to write to HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon. Check Permissions.");
}
}
///
/// Creates an Exe HardLink to the Application .exe at the Rooth Path
///
internal bool CreateExeHardLinkInRootPath
{
get
{
//fsutil hardlink create c:\foo.txt c:\bar.txt
bool bCreateHardLinkInRootPath = App.inifile.GetKeyValue(App.Ini_Setting.WindowsSettings__CreateExeHardLink_In_RootPath, true);
String FileNameWithExtension = AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameWithExtension(AssemblyW.AssemblyST.Entry);
String RootPath = PathNaming.GetPathRoot(AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameNPath(AssemblyW.AssemblyST.Entry));
String FileNameFullPath = AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameNPath(AssemblyW.AssemblyST.Entry);
if (bCreateHardLinkInRootPath && (String.Compare(FileNameFullPath, String.Format("{0}{1}", RootPath, FileNameWithExtension), true) != 0))
{
Process.Start(PStartInfo.CreateCMDDosCommandProcess(String.Format("del {0}{1}", RootPath, FileNameWithExtension), false));
Process.Start(PStartInfo.CreateCMDDosCommandProcess(String.Format("fsutil hardlink create {0}{1} \"{2}\"", RootPath, FileNameWithExtension, FileNameFullPath), false));
}
else if (String.Compare(FileNameFullPath, String.Format("{0}{1}", RootPath, FileNameWithExtension), true) != 0)
{
Process.Start(PStartInfo.CreateCMDDosCommandProcess(String.Format("del {0}{1}", "", ""), false));
}
return bCreateHardLinkInRootPath;
}
set
{
App.inifile.SetKeyValue(App.Ini_Setting.WindowsSettings__CreateExeHardLink_In_RootPath, value);
String FileNameWithExtension = AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameWithExtension(AssemblyW.AssemblyST.Entry);
String RootPath = PathNaming.GetPathRoot(AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameNPath(AssemblyW.AssemblyST.Entry));
String FileNameFullPath = AssemblyW.SpecializedAssemblyInfo.GetAssemblyFileNameNPath(AssemblyW.AssemblyST.Entry);
if (value && (String.Compare(FileNameFullPath, String.Format("{0}{1}", RootPath, FileNameWithExtension), true) != 0))
{
Process.Start(PStartInfo.CreateCMDDosCommandProcess(String.Format("del {0}\\{1}", RootPath, FileNameWithExtension), false));
Process.Start(PStartInfo.CreateCMDDosCommandProcess(String.Format("fsutil hardlink create {0}\\{1} \"{2}\"", RootPath, FileNameWithExtension, FileNameFullPath), false));
}
else if (String.Compare(FileNameFullPath, String.Format("{0}{1}", RootPath, FileNameWithExtension), true) != 0)
{
Process.Start(PStartInfo.CreateCMDDosCommandProcess(String.Format("del {0}\\{1}", "", ""), false));
}
}
}
#endregion
#region Prefill Settings
///
/// Prefill's the default exes for the Process Monitor (GUI Prefill Feature)
///
internal string DefaultExeRunningFilter
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Default_Exe_Running_Filter, AppResx.GetString("DEFAULT_EXE_RUNNING_FILTER"));
}
set
{
if(value != null)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Default_Exe_Running_Filter, value);
}
}
///
/// Prefill's the default services for the Service Monitor (GUI Prefill Feature)
///
internal string DefaultServicesNamesRunningFilter
{
get
{
return App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Default_Services_Names_Running_Filter, AppResx.GetString("DEFAULT_SERVICE_NAMES_RUNNING_FILTER"));
}
set
{
if(value != null)
App.inifile.SetKeyValue(App.Ini_Setting.MonitorSettings__Default_Services_Names_Running_Filter, value);
}
}
#endregion
#region Window Settings
///
/// Retrieve the Top N' Left for the Settings Window
///
/// Top Double
/// Left Double
internal void GetWindowSetting_TopLeft_SettingsWindow(ref double Top, ref double Left)
{
// Load the Window at the correct position
try
{
string TopLeft = App.inifile.GetKeyValue(App.Ini_Setting.WindowSettings__SettingsWindow_TopLeft, String.Empty);
if (!String.IsNullOrEmpty(TopLeft))
{
string[] tl = TopLeft.Split(';');
Top = double.Parse(tl[0]);
Left = double.Parse(tl[1]);
// Check Screen ! * To Do * Check Screen Dimensions *
}
}
catch (Exception ex) { App.log.Error("Error Loading Settings Window Settings", ex); }
}
///
/// Set the Top N' Left for the Settings Window
///
/// Top Double
/// Left Double
internal void SetWindowSetting_TopLeft_SettingsWindow(double Top, double Left)
{
if (!Double.IsNaN(Top) && !Double.IsNaN(Left))
App.inifile.SetKeyValue(App.Ini_Setting.WindowSettings__SettingsWindow_TopLeft, (Top.ToString() + ";" + Left.ToString()));
}
///
/// Retrieve the Top N' Left for the Settings Window
///
/// Top Double
/// Left Double
internal void GetWindowSetting_TopLeft_LogViewerWindow(ref double Top, ref double Left)
{
// Load the Window at the correct position
try
{
string TopLeft = App.inifile.GetKeyValue(App.Ini_Setting.WindowSettings__LogViewerWindow_TopLeft, String.Empty);
if (!String.IsNullOrEmpty(TopLeft))
{
string[] tl = TopLeft.Split(';');
Top = double.Parse(tl[0]);
Left = double.Parse(tl[1]);
// Check Screen ! * To Do * Check Screen Dimensions *
}
}
catch (Exception ex) { App.log.Error("Error Loading Settings Window Settings", ex); }
}
///
/// Set the Top N' Left for the Settings Window
///
/// Top Double
/// Left Double
internal void SetWindowSetting_TopLeft_LogViewerWindow(double Top, double Left)
{
if (!Double.IsNaN(Top) && !Double.IsNaN(Left))
App.inifile.SetKeyValue(App.Ini_Setting.WindowSettings__LogViewerWindow_TopLeft, (Top.ToString() + ";" + Left.ToString()));
}
///
/// Retrieve the Top N' Left for the Settings Window
///
/// Top Double
/// Left Double
internal void GetWindowSetting_TopLeft_AboutWindow(ref double Top, ref double Left)
{
// Load the Window at the correct position
try
{
string TopLeft = App.inifile.GetKeyValue(App.Ini_Setting.WindowSettings__AboutWindow_TopLeft, String.Empty);
if (!String.IsNullOrEmpty(TopLeft))
{
string[] tl = TopLeft.Split(';');
Top = double.Parse(tl[0]);
Left = double.Parse(tl[1]);
// Check Screen ! * To Do * Check Screen Dimensions *
}
}
catch (Exception ex) { App.log.Error("Error Loading Settings Window Settings", ex); }
}
///
/// Set the Top N' Left for the Settings Window
///
/// Top Double
/// Left Double
internal void SetWindowSetting_TopLeft_AboutWindow(double Top, double Left)
{
if (!Double.IsNaN(Top) && !Double.IsNaN(Left))
App.inifile.SetKeyValue(App.Ini_Setting.WindowSettings__AboutWindow_TopLeft, (Top.ToString() + ";" + Left.ToString()));
}
#endregion
#region Special Circumstances
///
/// HL7Messaging puts itself in the Windows\Run to start-up automatically,
/// If we monitor HL7Messaging then we should remove that key * Always * if we are to
/// start with windows. ~This function get's called in those instances where we should
/// check exactly that, and deal with exactly that issue.
///
internal void DeleteOffendingHL7KeyIfApplicable()
{
bool bStartWithWindowsIsSet = App.inifile.GetKeyValue(App.Ini_Setting.MonitorSettings__Start_With_Windows, true);
if (bStartWithWindowsIsSet)
{
WatchdogConfiguration config = App.xmlfile.ReadData(false);
bool bHasHL7MessagingConfigured = (config.MonitoredProcesses.GetProcessExesByProcessExeFileNameWithoutExtension("HL7Messaging").Length > 0);
if (bHasHL7MessagingConfigured) // Make sure the Key is Deleted
{
if (!RegKey.DeleteKey("Microsoft\\Windows\\CurrentVersion\\Run", "HL7Messenger"))
App.log.Error("Failed to write to Microsoft\\Windows\\CurrentVersion\\Run. Check Permissions.");
}
}
}
#endregion
}
}