diff --git a/ShareX.HelpersLib/Settings/SettingsBase.cs b/ShareX.HelpersLib/Settings/SettingsBase.cs index dcc665708..403bb2422 100644 --- a/ShareX.HelpersLib/Settings/SettingsBase.cs +++ b/ShareX.HelpersLib/Settings/SettingsBase.cs @@ -30,6 +30,7 @@ using System.ComponentModel; using System.IO; using System.Linq; +using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -109,6 +110,15 @@ public void SaveAsync() SaveAsync(FilePath); } + public MemoryStream SaveToMemoryStream(bool supportDPAPIEncryption = false) + { + ApplicationVersion = Application.ProductVersion; + + MemoryStream ms = new MemoryStream(); + SaveToStream(ms, supportDPAPIEncryption, true); + return ms; + } + private bool SaveInternal(string filePath) { string typeName = GetType().Name; @@ -127,25 +137,8 @@ private bool SaveInternal(string 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(); - - if (SupportDPAPIEncryption) - { - serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver(); - } - else - { - serializer.ContractResolver = new WritablePropertiesOnlyResolver(); - } - - serializer.Converters.Add(new StringEnumConverter()); - serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc; - serializer.Formatting = Formatting.Indented; - serializer.Serialize(jsonWriter, this); - jsonWriter.Flush(); + SaveToStream(fileStream, SupportDPAPIEncryption); } if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath)) @@ -195,6 +188,30 @@ private bool SaveInternal(string filePath) return isSuccess; } + private void SaveToStream(Stream stream, bool supportDPAPIEncryption = false, bool leaveOpen = false) + { + using (StreamWriter streamWriter = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, leaveOpen)) + using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter)) + { + JsonSerializer serializer = new JsonSerializer(); + + if (supportDPAPIEncryption) + { + serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver(); + } + else + { + serializer.ContractResolver = new WritablePropertiesOnlyResolver(); + } + + serializer.Converters.Add(new StringEnumConverter()); + serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc; + serializer.Formatting = Formatting.Indented; + serializer.Serialize(jsonWriter, this); + jsonWriter.Flush(); + } + } + public static T Load(string filePath, string backupFolder = null) { List fallbackFilePaths = new List(); diff --git a/ShareX.HelpersLib/Zip/ZipManager.cs b/ShareX.HelpersLib/Zip/ZipManager.cs index 5e9f464c9..7ec135dc0 100644 --- a/ShareX.HelpersLib/Zip/ZipManager.cs +++ b/ShareX.HelpersLib/Zip/ZipManager.cs @@ -187,6 +187,7 @@ private static ZipArchiveEntry CreateEntryFromStream(this ZipArchive archive, St using (Stream entryStream = entry.Open()) { + stream.Position = 0; stream.CopyTo(entryStream); } diff --git a/ShareX/SettingManager.cs b/ShareX/SettingManager.cs index 00582cb74..1363f810c 100644 --- a/ShareX/SettingManager.cs +++ b/ShareX/SettingManager.cs @@ -346,15 +346,22 @@ public static void ResetSettings() public static bool Export(string archivePath, bool settings, bool history) { + MemoryStream msApplicationConfig = null, msUploadersConfig = null, msHotkeysConfig = null; + try { List entries = new List(); if (settings) { - entries.Add(new ZipEntryInfo(ApplicationConfigFilePath)); - entries.Add(new ZipEntryInfo(HotkeysConfigFilePath)); - entries.Add(new ZipEntryInfo(UploadersConfigFilePath)); + msApplicationConfig = Settings.SaveToMemoryStream(false); + entries.Add(new ZipEntryInfo(msApplicationConfig, ApplicationConfigFilename)); + + msUploadersConfig = UploadersConfig.SaveToMemoryStream(false); + entries.Add(new ZipEntryInfo(msUploadersConfig, UploadersConfigFilename)); + + msHotkeysConfig = HotkeysConfig.SaveToMemoryStream(false); + entries.Add(new ZipEntryInfo(msHotkeysConfig, HotkeysConfigFilename)); } if (history) @@ -370,6 +377,12 @@ public static bool Export(string archivePath, bool settings, bool history) DebugHelper.WriteException(e); MessageBox.Show("Error while exporting backup:\r\n" + e, "ShareX - Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } + finally + { + msApplicationConfig?.Dispose(); + msUploadersConfig?.Dispose(); + msHotkeysConfig?.Dispose(); + } return false; }