mirror of
https://github.com/ShareX/ShareX.git
synced 2024-06-30 20:20:34 +12:00
#702: Improvements to setting save integrity
This commit is contained in:
parent
ca4e1f0ce6
commit
c63c37fd19
|
@ -144,7 +144,7 @@ private bool SaveInternal(string filePath)
|
|||
|
||||
string tempFilePath = filePath + ".temp";
|
||||
|
||||
using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
||||
using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
|
||||
using (StreamWriter streamWriter = new StreamWriter(fileStream))
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
|
||||
{
|
||||
|
@ -162,17 +162,16 @@ private bool SaveInternal(string filePath)
|
|||
throw new Exception($"{typeName} file is corrupt: {tempFilePath}");
|
||||
}
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
if (CreateBackup)
|
||||
{
|
||||
Helpers.CopyFile(filePath, BackupFolder);
|
||||
}
|
||||
string backupFilePath = null;
|
||||
|
||||
File.Delete(filePath);
|
||||
if (CreateBackup)
|
||||
{
|
||||
string fileName = Path.GetFileName(filePath);
|
||||
backupFilePath = Path.Combine(BackupFolder, fileName);
|
||||
Helpers.CreateDirectoryFromDirectoryPath(BackupFolder);
|
||||
}
|
||||
|
||||
File.Move(tempFilePath, filePath);
|
||||
File.Replace(tempFilePath, filePath, backupFilePath);
|
||||
|
||||
if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
|
||||
{
|
||||
|
|
|
@ -68,13 +68,13 @@ protected override bool Append(string filePath, IEnumerable<HistoryItem> history
|
|||
{
|
||||
Helpers.CreateDirectoryFromFilePath(filePath);
|
||||
|
||||
using (FileStream fs = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Read))
|
||||
using (StreamWriter sw = new StreamWriter(fs))
|
||||
using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
|
||||
using (StreamWriter streamWriter = new StreamWriter(fileStream))
|
||||
{
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
|
||||
|
||||
bool firstObject = fs.Length == 0;
|
||||
bool firstObject = fileStream.Length == 0;
|
||||
|
||||
foreach (HistoryItem historyItem in historyItems)
|
||||
{
|
||||
|
@ -87,7 +87,7 @@ protected override bool Append(string filePath, IEnumerable<HistoryItem> history
|
|||
|
||||
json += JObject.FromObject(historyItem, serializer).ToString();
|
||||
|
||||
sw.Write(json);
|
||||
streamWriter.Write(json);
|
||||
|
||||
firstObject = false;
|
||||
}
|
||||
|
|
|
@ -139,8 +139,8 @@ protected override bool Append(string filePath, IEnumerable<HistoryItem> history
|
|||
{
|
||||
Helpers.CreateDirectoryFromFilePath(filePath);
|
||||
|
||||
using (FileStream fs = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Read))
|
||||
using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8))
|
||||
using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
|
||||
using (XmlTextWriter writer = new XmlTextWriter(fileStream, Encoding.UTF8))
|
||||
{
|
||||
writer.Formatting = Formatting.Indented;
|
||||
writer.Indentation = 4;
|
||||
|
|
Loading…
Reference in a new issue