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).
This commit is contained in:
@@ -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 <mmoeller@openhardwaremonitor.org>
|
||||
Copyright (C) 2009-2014 Michael Möller <mmoeller@openhardwaremonitor.org>
|
||||
|
||||
*/
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user