From a5f5098beb32088cd83a8526030f1dc870ffd5a1 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Sun, 12 Feb 2017 20:03:32 +0000 Subject: [PATCH] backup existing broken file --- Wox.Infrastructure/Storage/JsonStorage.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Wox.Infrastructure/Storage/JsonStorage.cs b/Wox.Infrastructure/Storage/JsonStorage.cs index 29c6f509ef..abe1fd7b5e 100644 --- a/Wox.Infrastructure/Storage/JsonStorage.cs +++ b/Wox.Infrastructure/Storage/JsonStorage.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.IO; using Newtonsoft.Json; using Wox.Infrastructure.Logger; @@ -64,12 +65,24 @@ namespace Wox.Infrastructure.Storage } } - public void LoadDefault() + private void LoadDefault() { + BackupOriginFile(); _data = JsonConvert.DeserializeObject("{}", _serializerSettings); Save(); } + private void BackupOriginFile() + { + var timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fffffff", CultureInfo.CurrentUICulture); + var directory = Path.GetDirectoryName(FilePath).NonNull(); + var originName = Path.GetFileNameWithoutExtension(FilePath); + var backupName = $"{originName}-{timestamp}{FileSuffix}"; + var backupPath = Path.Combine(directory, backupName); + File.Copy(FilePath, backupPath, true); + // todo give user notification for the backup process + } + public void Save() { string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);