From eb3466a86bf085883e6ccf637698dd196cd78b9e Mon Sep 17 00:00:00 2001 From: RitchieF Date: Sun, 2 Apr 2017 20:46:37 +0200 Subject: [PATCH 1/2] Bugfix: Crash while saving when hosts was disabled and enabled together When the HostsFile was disabled (hosts.disabled file exists and no hosts file), saving would lead the program to create a new hosts file. If the user wanted to disable the HostsFile the again, the program would crash. Now before saving the program checks if there exists a hosts.disabled file and if yes, tells the user that he/she needs to enable the HostsFile before editing entries. --- src/HostsFile.cs | 5 +++++ src/MainForm.cs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/HostsFile.cs b/src/HostsFile.cs index ea1b131..bc963ba 100644 --- a/src/HostsFile.cs +++ b/src/HostsFile.cs @@ -182,6 +182,11 @@ public int LineCount } } + /// + /// Gets value indicating if the HostsFile is currently disabled. + /// + public static bool IsDisabled => File.Exists(DefaultDisabledHostFilePath); + #endregion #region Public Methods diff --git a/src/MainForm.cs b/src/MainForm.cs index 94177d7..9169000 100644 --- a/src/MainForm.cs +++ b/src/MainForm.cs @@ -614,6 +614,12 @@ private void OnSaveClick(object sender, EventArgs e) this.dataGridViewHostsEntries.CommitEdit( DataGridViewDataErrorContexts.Commit); + if (HostsFile.IsDisabled) + { + MessageBox.Show("The HostsFile is currently disabled. Enable the HostsFile before editing entries."); + return; + } + HostsFile.Instance.Save(); } From 07fcffff548fecccc76442ed7e0d2f3bc871fe3d Mon Sep 17 00:00:00 2001 From: RitchieF Date: Sun, 2 Apr 2017 21:01:50 +0200 Subject: [PATCH 2/2] Check for Enabled and Disabled HostsFile Added an additional check to make sure that the program doesn't crash when both enabled and disabled HostsFile exist at the same time. --- src/HostsFile.cs | 10 ++++++++++ src/MainForm.cs | 24 ++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/HostsFile.cs b/src/HostsFile.cs index bc963ba..2d5c1af 100644 --- a/src/HostsFile.cs +++ b/src/HostsFile.cs @@ -196,6 +196,11 @@ public int LineCount /// public static void DisableHostsFile() { + if (IsEnabled && IsDisabled) + { + throw new InvalidOperationException("The HostsFile is enabled and disabled at the same time."); + } + using (FileEx.DisableAttributes(DefaultHostFilePath, FileAttributes.ReadOnly)) { File.Move(DefaultHostFilePath, DefaultDisabledHostFilePath); @@ -208,6 +213,11 @@ public static void DisableHostsFile() /// public static void EnableHostsFile() { + if (IsEnabled && IsDisabled) + { + throw new InvalidOperationException("The HostsFile is enabled and disabled at the same time."); + } + using (FileEx.DisableAttributes(DefaultDisabledHostFilePath, FileAttributes.ReadOnly)) { File.Move(DefaultDisabledHostFilePath, DefaultHostFilePath); diff --git a/src/MainForm.cs b/src/MainForm.cs index 9169000..af66bbd 100644 --- a/src/MainForm.cs +++ b/src/MainForm.cs @@ -312,11 +312,31 @@ private void OnDisableHostsClick(object sender, EventArgs e) if (checkState) { - HostsFile.EnableHostsFile(); + try + { + HostsFile.EnableHostsFile(); + } + catch (InvalidOperationException invalidOperationException) + { + Console.WriteLine(invalidOperationException); + MessageBox.Show(invalidOperationException.Message, "Error during operation!", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } } else { - HostsFile.DisableHostsFile(); + try + { + HostsFile.DisableHostsFile(); + } + catch (InvalidOperationException invalidOperationException) + { + Console.WriteLine(invalidOperationException); + MessageBox.Show(invalidOperationException.Message, "Error during operation!", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } } this.UpdateNotifyIcon();