# HG changeset patch # User moel.mich # Date 1419960929 0 # Node ID 5d87c9fc69f68ce7974fd8d074bd670e32e26ee3 # Parent 3c1cdc197b24b5fe8ebbc5a2d21a08c9ebc30066 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). diff -r 3c1cdc197b24 -r 5d87c9fc69f6 Properties/AssemblyVersion.cs --- a/Properties/AssemblyVersion.cs Tue Dec 30 16:02:52 2014 +0000 +++ b/Properties/AssemblyVersion.cs Tue Dec 30 17:35:29 2014 +0000 @@ -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 -r 3c1cdc197b24 -r 5d87c9fc69f6 Utilities/PersistentSettings.cs --- a/Utilities/PersistentSettings.cs Tue Dec 30 16:02:52 2014 +0000 +++ b/Utilities/PersistentSettings.cs Tue Dec 30 17:35:29 2014 +0000 @@ -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.Drawing; using System.Globalization; -using System.Drawing; +using System.IO; +using System.Text; using System.Xml; using OpenHardwareMonitor.Hardware; @@ -25,8 +27,22 @@ 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 @@ } } } - } + } } 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 @@ 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) {