private bool SaveInternal(string filePath) { string typeName = GetType().Name; DebugHelper.WriteLine($"{typeName} save started: {filePath}"); bool isSuccess = false; try { if (!string.IsNullOrEmpty(filePath)) { lock (this) { Helpers.CreateDirectoryFromFilePath(filePath); string tempFilePath = filePath + ".temp"; 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)) { JsonSerializer serializer = new JsonSerializer(); serializer.ContractResolver = new WritablePropertiesOnlyResolver(); serializer.Converters.Add(new StringEnumConverter()); serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc; serializer.Formatting = Formatting.Indented; serializer.Serialize(jsonWriter, this); jsonWriter.Flush(); } if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath)) { throw new Exception($"{typeName} file is corrupt: {tempFilePath}"); } string backupFilePath = null; if (CreateBackup) { string fileName = Path.GetFileName(filePath); backupFilePath = Path.Combine(BackupFolder, fileName); Helpers.CreateDirectoryFromDirectoryPath(BackupFolder); } File.Replace(tempFilePath, filePath, backupFilePath); if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder)) { Helpers.BackupFileWeekly(filePath, BackupFolder); } isSuccess = true; } } } catch (Exception e) { DebugHelper.WriteException(e); OnSettingsSaveFailed(e); } finally { string status = isSuccess ? "successful" : "failed"; DebugHelper.WriteLine($"{typeName} save {status}: {filePath}"); } return(isSuccess); }
private bool SaveInternal(string filePath) { string typeName = GetType().Name; DebugHelper.WriteLine($"{typeName} save started: {filePath}"); bool isSuccess = false; try { if (!string.IsNullOrEmpty(filePath)) { lock (this) { Helpers.CreateDirectoryFromFilePath(filePath); string tempFilePath = filePath + ".temp"; using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough)) { SaveToStream(fileStream, SupportDPAPIEncryption); } if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath)) { throw new Exception($"{typeName} file is corrupt: {tempFilePath}"); } if (File.Exists(filePath)) { string backupFilePath = null; if (CreateBackup) { string fileName = Path.GetFileName(filePath); backupFilePath = Path.Combine(BackupFolder, fileName); Helpers.CreateDirectory(BackupFolder); } File.Replace(tempFilePath, filePath, backupFilePath); } else { File.Move(tempFilePath, filePath); } if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder)) { Helpers.BackupFileWeekly(filePath, BackupFolder); } isSuccess = true; } } } catch (Exception e) { DebugHelper.WriteException(e); OnSettingsSaveFailed(e); } finally { string status = isSuccess ? "successful" : "failed"; DebugHelper.WriteLine($"{typeName} save {status}: {filePath}"); } return(isSuccess); }