From 57ea8d60cc71688b23aea7541425951544a902f5 Mon Sep 17 00:00:00 2001 From: Markus Remmel Date: Fri, 4 Sep 2015 16:09:54 +0200 Subject: [PATCH 1/3] If the user.config file is corrupt (e.g. empty) the whole application crashes I had the problem, that a customer could suddenly not open my application anymore. It turned out that this is because the user.config file was just empty and this caused a ConfigurationException. Then I added an UnhandledExceptionHandler but this exception seems to happen earlier. The only working solution ist to delete the odd user.config and restart the application. For more details see here: http://www.codeproject.com/Articles/30216/Handling-Corrupt-user-config-Settings --- MahApps.Metro/Controls/WindowSettings.cs | 16 ++++++++++++++-- MahApps.Metro/MahApps.Metro.NET45.csproj | 1 + MahApps.Metro/MahApps.Metro.csproj | 1 + samples/MetroDemo/MainWindow.xaml | 1 + 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/MahApps.Metro/Controls/WindowSettings.cs b/MahApps.Metro/Controls/WindowSettings.cs index a0dd732750..cc8ba0d5d7 100644 --- a/MahApps.Metro/Controls/WindowSettings.cs +++ b/MahApps.Metro/Controls/WindowSettings.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Configuration; using System.Diagnostics; +using System.IO; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; @@ -64,9 +65,20 @@ public bool UpgradeSettings { get { - if (this["UpgradeSettings"] != null) + try { - return (bool)this["UpgradeSettings"]; + if (this["UpgradeSettings"] != null) + { + return (bool)this["UpgradeSettings"]; + } + } + catch (Exception ex) + { + Debug.WriteLine("Failed to load settings file:\r\n{0}", ex); + var filename = ((ConfigurationErrorsException)ex.InnerException).Filename; + File.Delete(filename); + Process.Start(Application.ResourceAssembly.Location); + Application.Current.Shutdown(); } return true; } diff --git a/MahApps.Metro/MahApps.Metro.NET45.csproj b/MahApps.Metro/MahApps.Metro.NET45.csproj index 28d66e0c02..6da72a5391 100644 --- a/MahApps.Metro/MahApps.Metro.NET45.csproj +++ b/MahApps.Metro/MahApps.Metro.NET45.csproj @@ -106,6 +106,7 @@ + False ..\Lib\NET45\System.Windows.Interactivity.dll diff --git a/MahApps.Metro/MahApps.Metro.csproj b/MahApps.Metro/MahApps.Metro.csproj index a5e632b82b..3a23e98a1d 100644 --- a/MahApps.Metro/MahApps.Metro.csproj +++ b/MahApps.Metro/MahApps.Metro.csproj @@ -68,6 +68,7 @@ + False ..\Lib\NET40\System.Windows.Interactivity.dll diff --git a/samples/MetroDemo/MainWindow.xaml b/samples/MetroDemo/MainWindow.xaml index 054418aa89..43fd69da01 100644 --- a/samples/MetroDemo/MainWindow.xaml +++ b/samples/MetroDemo/MainWindow.xaml @@ -22,6 +22,7 @@ d:DataContext="{d:DesignInstance MetroDemo:MainWindowViewModel}" Closing="MetroWindow_Closing" Dialog:DialogParticipation.Register="{Binding}" + SaveWindowPosition="True" > From 5bf17bec660767f27e1185315418d59c5d25679e Mon Sep 17 00:00:00 2001 From: Markus Remmel Date: Fri, 4 Sep 2015 17:30:47 +0200 Subject: [PATCH 2/3] Throwing MahAppsExceotion The ConfigurationErrorsException can't be catched by an UnhandledExceptionHandler. So it is better to wrap it into a MahAppsException --- MahApps.Metro/Controls/WindowSettings.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/MahApps.Metro/Controls/WindowSettings.cs b/MahApps.Metro/Controls/WindowSettings.cs index cc8ba0d5d7..5c2c81f028 100644 --- a/MahApps.Metro/Controls/WindowSettings.cs +++ b/MahApps.Metro/Controls/WindowSettings.cs @@ -72,13 +72,10 @@ public bool UpgradeSettings return (bool)this["UpgradeSettings"]; } } - catch (Exception ex) + catch (ConfigurationErrorsException ex) { - Debug.WriteLine("Failed to load settings file:\r\n{0}", ex); var filename = ((ConfigurationErrorsException)ex.InnerException).Filename; - File.Delete(filename); - Process.Start(Application.ResourceAssembly.Location); - Application.Current.Shutdown(); + throw new MahAppsException(string.Format("The settings file {0} seems to be corrupted", filename), ex); } return true; } From 96a52f1208106eda259080b3b18f333f1d6d9dc8 Mon Sep 17 00:00:00 2001 From: Markus Remmel Date: Fri, 4 Sep 2015 17:38:33 +0200 Subject: [PATCH 3/3] Removed reference to System.IO --- MahApps.Metro/Controls/WindowSettings.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/MahApps.Metro/Controls/WindowSettings.cs b/MahApps.Metro/Controls/WindowSettings.cs index 5c2c81f028..1e9d5db3fa 100644 --- a/MahApps.Metro/Controls/WindowSettings.cs +++ b/MahApps.Metro/Controls/WindowSettings.cs @@ -2,7 +2,6 @@ using System.ComponentModel; using System.Configuration; using System.Diagnostics; -using System.IO; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop;