From ebeb96c648571655cf620b7fe68dd8499f65f0a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=B6ller?= Date: Tue, 30 Dec 2014 17:35:29 +0000 Subject: [PATCH] Changed the settings save code to use a two file based approach in order to reduce cases where settings are lost or reset (Fixed Issue 501). --- Properties/AssemblyVersion.cs | 4 +-- Utilities/PersistentSettings.cs | 54 ++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/Properties/AssemblyVersion.cs b/Properties/AssemblyVersion.cs index 57e2043..3ea6451 100644 --- a/Properties/AssemblyVersion.cs +++ b/Properties/AssemblyVersion.cs @@ -10,5 +10,5 @@ using System.Reflection; -[assembly: AssemblyVersion("0.6.0.16")] -[assembly: AssemblyInformationalVersion("0.6.0.16 Alpha")] \ No newline at end of file +[assembly: AssemblyVersion("0.6.0.17")] +[assembly: AssemblyInformationalVersion("0.6.0.17 Alpha")] \ No newline at end of file diff --git a/Utilities/PersistentSettings.cs b/Utilities/PersistentSettings.cs index f68a1c5..1694aeb 100644 --- a/Utilities/PersistentSettings.cs +++ b/Utilities/PersistentSettings.cs @@ -4,13 +4,15 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - Copyright (C) 2009-2010 Michael Möller + Copyright (C) 2009-2014 Michael Möller */ using System.Collections.Generic; -using System.Globalization; using System.Drawing; +using System.Globalization; +using System.IO; +using System.Text; using System.Xml; using OpenHardwareMonitor.Hardware; @@ -25,8 +27,22 @@ namespace OpenHardwareMonitor { try { doc.Load(fileName); } catch { - return; + try { + File.Delete(fileName); + } catch { } + + string backupFileName = fileName + ".backup"; + try { + doc.Load(backupFileName); + } catch { + try { + File.Delete(backupFileName); + } catch { } + + return; + } } + XmlNodeList list = doc.GetElementsByTagName("appSettings"); foreach (XmlNode node in list) { XmlNode parent = node.ParentNode; @@ -44,10 +60,11 @@ namespace OpenHardwareMonitor { } } } - } + } } public void Save(string fileName) { + XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); XmlElement configuration = doc.CreateElement("configuration"); @@ -60,7 +77,34 @@ namespace OpenHardwareMonitor { add.SetAttribute("value", keyValuePair.Value); appSettings.AppendChild(add); } - doc.Save(fileName); + + byte[] file; + using (var memory = new MemoryStream()) { + using (var writer = new StreamWriter(memory, Encoding.UTF8)) { + doc.Save(writer); + } + file = memory.ToArray(); + } + + string backupFileName = fileName + ".backup"; + if (File.Exists(fileName)) { + try { + File.Delete(backupFileName); + } catch { } + try { + File.Move(fileName, backupFileName); + } catch { } + } + + using (var stream = new FileStream(fileName, + FileMode.Create, FileAccess.Write)) + { + stream.Write(file, 0, file.Length); + } + + try { + File.Delete(backupFileName); + } catch { } } public bool Contains(string name) {