mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-05 20:54:31 +13:00
Save settings to "filename.json.temp" file and rename it to actual file name for decrease probability of save corruption happen when pc shutdown
This commit is contained in:
parent
551a47bac1
commit
8447e68548
1 changed files with 14 additions and 5 deletions
|
@ -57,17 +57,26 @@ public static bool Save(object obj, string filePath, SerializationType type, boo
|
||||||
{
|
{
|
||||||
lock (obj)
|
lock (obj)
|
||||||
{
|
{
|
||||||
using (MemoryStream ms = new MemoryStream())
|
string tempFilePath = filePath + ".temp";
|
||||||
{
|
|
||||||
Save(obj, ms, type);
|
|
||||||
|
|
||||||
if (createBackup && File.Exists(filePath))
|
using (FileStream fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
||||||
|
{
|
||||||
|
Save(obj, fs, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (File.Exists(filePath))
|
||||||
|
{
|
||||||
|
if (createBackup)
|
||||||
{
|
{
|
||||||
File.Copy(filePath, filePath + ".bak", true);
|
File.Copy(filePath, filePath + ".bak", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
isSuccess = ms.WriteToFile(filePath);
|
File.Delete(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File.Move(tempFilePath, filePath);
|
||||||
|
|
||||||
|
isSuccess = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue