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).
authormoel.mich
Tue, 30 Dec 2014 17:35:29 +0000
changeset 4295d87c9fc69f6
parent 428 3c1cdc197b24
child 430 6b24e39f1b84
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
Utilities/PersistentSettings.cs
     1.1 --- a/Properties/AssemblyVersion.cs	Tue Dec 30 16:02:52 2014 +0000
     1.2 +++ b/Properties/AssemblyVersion.cs	Tue Dec 30 17:35:29 2014 +0000
     1.3 @@ -10,5 +10,5 @@
     1.4  
     1.5  using System.Reflection;
     1.6  
     1.7 -[assembly: AssemblyVersion("0.6.0.16")]
     1.8 -[assembly: AssemblyInformationalVersion("0.6.0.16 Alpha")]
     1.9 \ No newline at end of file
    1.10 +[assembly: AssemblyVersion("0.6.0.17")]
    1.11 +[assembly: AssemblyInformationalVersion("0.6.0.17 Alpha")]
    1.12 \ No newline at end of file
     2.1 --- a/Utilities/PersistentSettings.cs	Tue Dec 30 16:02:52 2014 +0000
     2.2 +++ b/Utilities/PersistentSettings.cs	Tue Dec 30 17:35:29 2014 +0000
     2.3 @@ -4,13 +4,15 @@
     2.4    License, v. 2.0. If a copy of the MPL was not distributed with this
     2.5    file, You can obtain one at http://mozilla.org/MPL/2.0/.
     2.6   
     2.7 -  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     2.8 +  Copyright (C) 2009-2014 Michael Möller <mmoeller@openhardwaremonitor.org>
     2.9  	
    2.10  */
    2.11  
    2.12  using System.Collections.Generic;
    2.13 +using System.Drawing;
    2.14  using System.Globalization;
    2.15 -using System.Drawing;
    2.16 +using System.IO;
    2.17 +using System.Text;
    2.18  using System.Xml;
    2.19  using OpenHardwareMonitor.Hardware;
    2.20  
    2.21 @@ -25,8 +27,22 @@
    2.22        try {
    2.23          doc.Load(fileName);
    2.24        } catch {
    2.25 -        return;
    2.26 +        try {
    2.27 +          File.Delete(fileName);
    2.28 +        } catch { }
    2.29 +
    2.30 +        string backupFileName = fileName + ".backup";
    2.31 +        try {
    2.32 +          doc.Load(backupFileName);
    2.33 +        } catch {
    2.34 +          try {
    2.35 +            File.Delete(backupFileName);
    2.36 +          } catch { }
    2.37 +
    2.38 +          return;
    2.39 +        }
    2.40        }
    2.41 +
    2.42        XmlNodeList list = doc.GetElementsByTagName("appSettings");
    2.43        foreach (XmlNode node in list) {
    2.44          XmlNode parent = node.ParentNode;
    2.45 @@ -44,10 +60,11 @@
    2.46              }
    2.47            }
    2.48          }
    2.49 -      }      
    2.50 +      }
    2.51      }
    2.52  
    2.53      public void Save(string fileName) {
    2.54 +
    2.55        XmlDocument doc = new XmlDocument();
    2.56        doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
    2.57        XmlElement configuration = doc.CreateElement("configuration");
    2.58 @@ -60,7 +77,34 @@
    2.59          add.SetAttribute("value", keyValuePair.Value);
    2.60          appSettings.AppendChild(add);
    2.61        }
    2.62 -      doc.Save(fileName);
    2.63 +
    2.64 +      byte[] file;
    2.65 +      using (var memory = new MemoryStream()) {
    2.66 +        using (var writer = new StreamWriter(memory, Encoding.UTF8)) {
    2.67 +          doc.Save(writer);
    2.68 +        }
    2.69 +        file = memory.ToArray();
    2.70 +      }
    2.71 +
    2.72 +      string backupFileName = fileName + ".backup";
    2.73 +      if (File.Exists(fileName)) {
    2.74 +        try {
    2.75 +          File.Delete(backupFileName);
    2.76 +        } catch { }
    2.77 +        try {
    2.78 +          File.Move(fileName, backupFileName);
    2.79 +        } catch { }
    2.80 +      }
    2.81 +
    2.82 +      using (var stream = new FileStream(fileName, 
    2.83 +        FileMode.Create, FileAccess.Write))
    2.84 +      {
    2.85 +        stream.Write(file, 0, file.Length);
    2.86 +      }
    2.87 +
    2.88 +      try {
    2.89 +        File.Delete(backupFileName);
    2.90 +      } catch { }
    2.91      }
    2.92  
    2.93      public bool Contains(string name) {