annotate ServerMonitor/Forms/SettingsForm.cs @ 23:3866c19535fd

Fix NullReferenceException when checks are executed on a brand new server.
author Brad Greco <brad@bgreco.net>
date Thu, 30 May 2019 21:40:27 -0400
parents 7626b099aefd
children f6235dc0a8ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
1 using Microsoft.Win32;
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
2 using ServerMonitorApp.Properties;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.Text.RegularExpressions;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Windows.Forms;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 namespace ServerMonitorApp
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 {
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
9 /// <summary>Application settings form.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 public partial class SettingsForm : Form
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 {
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
12 private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
13 private readonly string autorunName = "ServerMonitor";
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
14
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 public SettingsForm()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 InitializeComponent();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 private void SettingsForm_Load(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 {
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
22 Icon = Resources.icon;
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
23 // Populate each action combo box with the available actions to perform on failure.
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox })
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 comboBox.DataSource = Enum.GetValues(typeof(FailAction));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 comboBox.Format += FailActionComboBox_Format;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 }
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
29 // Initialize controls with current settings.
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
30 AutorunCheckBox.Checked = GetAutorun();
11
75ca86e0862c Add setting to hide to notification area.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
31 KeepRunningCheckBox.Checked = Settings.Default.HideToNotificationArea;
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
32 KeepLogDaysInput.Value = Settings.Default.KeepLogDays;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 WarningComboBox.SelectedItem = Settings.Default.WarningAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 InformationComboBox.SelectedItem = Settings.Default.InformationAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
38 /// <summary>Gets whether an autorun registry key for this program exists.</summary>
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
39 /// <returns>Whether an autorun registry key for this program exists</returns>
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
40 private bool GetAutorun()
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
41 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
42 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
43 string value = (string)key.GetValue(autorunName, string.Empty);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
44 return value.StartsWith(Application.ExecutablePath);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
45 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
46
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
47 /// <summary>Sets whether this program should automatically start with Windows.</summary>
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
48 /// <param name="autorun">Whether autorun should be enabled or disabled.</param>
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
49 private void SetAutorun(bool autorun)
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
50 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
51 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, true);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
52 if (autorun)
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
53 key.SetValue(autorunName, Application.ExecutablePath.ToString());
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
54 else
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
55 key.DeleteValue(autorunName, false);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
56 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
57
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
58 /// <summary>Shows a human readable description of possible check failure actions in the combo box.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 {
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
61 // Transform the "CamelCase" enum name in to a "Camel case" name, adding
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
62 // spaces and making all characters besides the first lower case.
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
66 /// <summary>Saves the user settings and closes the form.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 private void OkButton_Click(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 {
11
75ca86e0862c Add setting to hide to notification area.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
69 Settings.Default.HideToNotificationArea = KeepRunningCheckBox.Checked;
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
70 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 Settings.Default.Save();
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
75 SetAutorun(AutorunCheckBox.Checked);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 Close();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78
16
7626b099aefd More comments.
Brad Greco <brad@bgreco.net>
parents: 11
diff changeset
79 /// <summary>Closes the form without saving settings.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 private void CancelSettingsButton_Click(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 Close();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 }