diff ServerMonitor/Forms/SettingsForm.cs @ 8:052aa62cb42a

Single instance. Add autorun option. Add icons. General enhancements.
author Brad Greco <brad@bgreco.net>
date Sat, 09 Mar 2019 20:14:03 -0500
parents c1dffaac66fa
children 75ca86e0862c
line wrap: on
line diff
--- a/ServerMonitor/Forms/SettingsForm.cs	Fri Mar 01 21:39:22 2019 -0500
+++ b/ServerMonitor/Forms/SettingsForm.cs	Sat Mar 09 20:14:03 2019 -0500
@@ -1,4 +1,5 @@
-using ServerMonitorApp.Properties;
+using Microsoft.Win32;
+using ServerMonitorApp.Properties;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
@@ -14,6 +15,9 @@
 {
     public partial class SettingsForm : Form
     {
+        private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
+        private readonly string autorunName = "ServerMonitor";
+
         public SettingsForm()
         {
             InitializeComponent();
@@ -21,17 +25,35 @@
 
         private void SettingsForm_Load(object sender, EventArgs e)
         {
+            Icon = Resources.icon;
             foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox })
             {
                 comboBox.DataSource = Enum.GetValues(typeof(FailAction));
                 comboBox.Format += FailActionComboBox_Format;
             }
+            AutorunCheckBox.Checked = GetAutorun();
             KeepLogDaysInput.Value = Settings.Default.KeepLogDays;
             ErrorComboBox.SelectedItem = Settings.Default.ErrorAction;
             WarningComboBox.SelectedItem = Settings.Default.WarningAction;
             InformationComboBox.SelectedItem = Settings.Default.InformationAction;
         }
 
+        private bool GetAutorun()
+        {
+            RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false);
+            string value = (string)key.GetValue(autorunName, string.Empty);
+            return value.StartsWith(Application.ExecutablePath);
+        }
+
+        private void SetAutorun(bool autorun)
+        {
+            RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, true);
+            if (autorun)
+                key.SetValue(autorunName, Application.ExecutablePath.ToString());
+            else
+                key.DeleteValue(autorunName, false);
+        }
+
         private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e)
         {
             e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1);
@@ -44,6 +66,7 @@
             Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem;
             Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem;
             Settings.Default.Save();
+            SetAutorun(AutorunCheckBox.Checked);
             Close();
         }