Fix crash issues when deserialize failed

This commit is contained in:
qianlifeng 2015-01-04 18:14:50 +08:00
parent 4243843951
commit 4d65b4c7a5
3 changed files with 17 additions and 7 deletions

View File

@ -79,7 +79,7 @@ namespace Wox.Infrastructure.Storage
/// <returns></returns>
protected virtual T LoadDefault()
{
return serializedObject;
return new T();
}
protected abstract void LoadInternal();

View File

@ -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)
{

View File

@ -153,7 +153,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
{
DontPromptUpdateMsg = false;
Theme = "Dark";
Language = "English";
Language = "en";
ReplaceWinR = true;
WebSearches = LoadDefaultWebSearches();
ProgramSources = new List<ProgramSource>();
@ -189,7 +189,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
}
if (storage.Language == null)
{
storage.Language = "English";
storage.Language = "en";
}
}
}