Updated the version.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
11 using System.Collections.Generic;
12 using System.Globalization;
15 using OpenHardwareMonitor.Hardware;
17 namespace OpenHardwareMonitor {
18 public class PersistentSettings : ISettings {
20 private IDictionary<string, string> settings =
21 new Dictionary<string, string>();
23 public void Load(string fileName) {
24 XmlDocument doc = new XmlDocument();
30 XmlNodeList list = doc.GetElementsByTagName("appSettings");
31 foreach (XmlNode node in list) {
32 XmlNode parent = node.ParentNode;
33 if (parent != null && parent.Name == "configuration" &&
34 parent.ParentNode is XmlDocument) {
35 foreach (XmlNode child in node.ChildNodes) {
36 if (child.Name == "add") {
37 XmlAttributeCollection attributes = child.Attributes;
38 XmlAttribute keyAttribute = attributes["key"];
39 XmlAttribute valueAttribute = attributes["value"];
40 if (keyAttribute != null && valueAttribute != null &&
41 keyAttribute.Value != null) {
42 settings.Add(keyAttribute.Value, valueAttribute.Value);
50 public void Save(string fileName) {
51 XmlDocument doc = new XmlDocument();
52 doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
53 XmlElement configuration = doc.CreateElement("configuration");
54 doc.AppendChild(configuration);
55 XmlElement appSettings = doc.CreateElement("appSettings");
56 configuration.AppendChild(appSettings);
57 foreach (KeyValuePair<string, string> keyValuePair in settings) {
58 XmlElement add = doc.CreateElement("add");
59 add.SetAttribute("key", keyValuePair.Key);
60 add.SetAttribute("value", keyValuePair.Value);
61 appSettings.AppendChild(add);
66 public bool Contains(string name) {
67 return settings.ContainsKey(name);
70 public void SetValue(string name, string value) {
71 settings[name] = value;
74 public string GetValue(string name, string value) {
76 if (settings.TryGetValue(name, out result))
82 public void Remove(string name) {
83 settings.Remove(name);
86 public void SetValue(string name, int value) {
87 settings[name] = value.ToString();
90 public int GetValue(string name, int value) {
92 if (settings.TryGetValue(name, out str)) {
94 if (int.TryParse(str, out parsedValue))
103 public void SetValue(string name, float value) {
104 settings[name] = value.ToString(CultureInfo.InvariantCulture);
107 public float GetValue(string name, float value) {
109 if (settings.TryGetValue(name, out str)) {
111 if (float.TryParse(str, NumberStyles.Float,
112 CultureInfo.InvariantCulture, out parsedValue))
121 public void SetValue(string name, bool value) {
122 settings[name] = value ? "true" : "false";
125 public bool GetValue(string name, bool value) {
127 if (settings.TryGetValue(name, out str)) {
128 return str == "true";
134 public void SetValue(string name, Color color) {
135 settings[name] = color.ToArgb().ToString("X8");
138 public Color GetValue(string name, Color value) {
140 if (settings.TryGetValue(name, out str)) {
142 if (int.TryParse(str, NumberStyles.HexNumber,
143 CultureInfo.InvariantCulture, out parsedValue))
144 return Color.FromArgb(parsedValue);