Added extra measure to stop corrupting backup of setting files

This commit is contained in:
Jaex 2020-05-07 10:38:00 +03:00
parent 775cfc5275
commit 14b9491664
2 changed files with 28 additions and 0 deletions

View file

@ -75,5 +75,28 @@ public static T DeserializeFromFilePath<T>(string filePath)
return default(T);
}
public static bool QuickVerifyJsonFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
if (fs.Length > 1 && fs.ReadByte() == (byte)'{')
{
fs.Seek(-1, SeekOrigin.End);
return fs.ReadByte() == (byte)'}';
}
}
}
}
catch
{
}
return false;
}
}
}

View file

@ -157,6 +157,11 @@ private bool SaveInternal(string filePath)
jsonWriter.Flush();
}
if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
{
throw new Exception($"{typeName} file is corrupt.");
}
if (File.Exists(filePath))
{
if (CreateBackup)