From 4d65b4c7a5b3b499852d4b5982b2f61dc02a2504 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sun, 4 Jan 2015 18:14:50 +0800 Subject: [PATCH] Fix crash issues when deserialize failed --- Wox.Infrastructure/Storage/BaseStorage.cs | 2 +- Wox.Infrastructure/Storage/BinaryStorage.cs | 18 ++++++++++++++---- .../Storage/UserSettings/UserSettingStorage.cs | 4 ++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Wox.Infrastructure/Storage/BaseStorage.cs b/Wox.Infrastructure/Storage/BaseStorage.cs index c72bf63c35..1b6c7c078e 100644 --- a/Wox.Infrastructure/Storage/BaseStorage.cs +++ b/Wox.Infrastructure/Storage/BaseStorage.cs @@ -79,7 +79,7 @@ namespace Wox.Infrastructure.Storage /// protected virtual T LoadDefault() { - return serializedObject; + return new T(); } protected abstract void LoadInternal(); diff --git a/Wox.Infrastructure/Storage/BinaryStorage.cs b/Wox.Infrastructure/Storage/BinaryStorage.cs index 39f1a43b7a..c0e8be63cf 100644 --- a/Wox.Infrastructure/Storage/BinaryStorage.cs +++ b/Wox.Infrastructure/Storage/BinaryStorage.cs @@ -25,10 +25,20 @@ namespace Wox.Infrastructure.Storage { try { - FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - BinaryFormatter binaryFormatter = new BinaryFormatter(); - serializedObject = binaryFormatter.Deserialize(fileStream) as T; - fileStream.Close(); + using (FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + BinaryFormatter binaryFormatter = new BinaryFormatter(); + serializedObject = binaryFormatter.Deserialize(fileStream) as T; + if (serializedObject == null) + { + serializedObject = LoadDefault(); +#if (DEBUG) + { + throw new Exception("deserialize failed"); + } +#endif + } + } } catch (Exception) { diff --git a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs index f7bf665525..1dd42a4044 100644 --- a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs +++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs @@ -153,7 +153,7 @@ namespace Wox.Infrastructure.Storage.UserSettings { DontPromptUpdateMsg = false; Theme = "Dark"; - Language = "English"; + Language = "en"; ReplaceWinR = true; WebSearches = LoadDefaultWebSearches(); ProgramSources = new List(); @@ -189,7 +189,7 @@ namespace Wox.Infrastructure.Storage.UserSettings } if (storage.Language == null) { - storage.Language = "English"; + storage.Language = "en"; } } }