Fixed Issue 72.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
43 namespace OpenHardwareMonitor.Utilities {
45 public sealed class Config {
46 private static readonly Config instance = new Config();
48 private string fileName;
50 private System.Configuration.Configuration config;
53 this.fileName = Path.ChangeExtension(
54 System.Windows.Forms.Application.ExecutablePath, ".config");
55 System.Configuration.ExeConfigurationFileMap fileMap =
56 new System.Configuration.ExeConfigurationFileMap();
57 fileMap.ExeConfigFilename = fileName;
58 config = System.Configuration.ConfigurationManager.
59 OpenMappedExeConfiguration(fileMap,
60 System.Configuration.ConfigurationUserLevel.None);
63 private void SaveConfig() {
64 string tempName = Path.ChangeExtension(fileName, ".tmp");
66 if (File.Exists(tempName))
67 File.Delete(tempName);
69 config.SaveAs(tempName);
70 if (File.Exists(fileName) && File.Exists(tempName))
71 File.Delete(fileName);
72 File.Move(tempName, fileName);
73 } catch (System.Configuration.ConfigurationErrorsException) { }
76 public static void Save() {
77 instance.SaveConfig();
80 public static Config Settings {
86 public string this[string name] {
88 System.Configuration.KeyValueConfigurationElement element =
89 config.AppSettings.Settings[name];
96 config.AppSettings.Settings.Remove(name);
97 config.AppSettings.Settings.Add(name, value);
101 public static bool Contains(string name) {
102 System.Configuration.KeyValueConfigurationElement element =
103 instance.config.AppSettings.Settings[name];
104 return element != null;
107 public static void Remove(string name) {
108 instance.config.AppSettings.Settings.Remove(name);
111 public static void Set(string name, bool value) {
112 instance[name] = value ? "true" : "false";
115 public static bool Get(string name, bool value) {
116 System.Configuration.KeyValueConfigurationElement element =
117 instance.config.AppSettings.Settings[name];
121 return element.Value == "true";
124 public static void Set(string name, int value) {
125 instance[name] = value.ToString();
128 public static int Get(string name, int value) {
129 System.Configuration.KeyValueConfigurationElement element =
130 instance.config.AppSettings.Settings[name];
135 if (int.TryParse(element.Value, out parsedValue))
142 public static void Set(string name, Color color) {
143 instance[name] = color.ToArgb().ToString("X8");
146 public static Color Get(string name, Color value) {
147 System.Configuration.KeyValueConfigurationElement element =
148 instance.config.AppSettings.Settings[name];
153 if (int.TryParse(element.Value,
154 System.Globalization.NumberStyles.HexNumber,
155 System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
156 return Color.FromArgb(parsedValue);
162 public static void Set(string name, float value) {
163 instance[name] = value.ToString(
164 System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
167 public static float Get(string name, float value) {
168 System.Configuration.KeyValueConfigurationElement element =
169 instance.config.AppSettings.Settings[name];
174 if (float.TryParse(element.Value,
175 System.Globalization.NumberStyles.Float,
176 System.Globalization.CultureInfo.InvariantCulture, out parsedValue))