Fixed Issue 25.
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);
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 Config Settings {
82 public string this[string name] {
84 System.Configuration.KeyValueConfigurationElement element =
85 config.AppSettings.Settings[name];
92 config.AppSettings.Settings.Remove(name);
93 config.AppSettings.Settings.Add(name, value);
97 public static bool Contains(string name) {
98 System.Configuration.KeyValueConfigurationElement element =
99 instance.config.AppSettings.Settings[name];
100 return element != null;
103 public static void Remove(string name) {
104 instance.config.AppSettings.Settings.Remove(name);
107 public static void Set(string name, bool value) {
108 instance[name] = value ? "true" : "false";
111 public static bool Get(string name, bool value) {
112 System.Configuration.KeyValueConfigurationElement element =
113 instance.config.AppSettings.Settings[name];
117 return element.Value == "true";
120 public static void Set(string name, int value) {
121 instance[name] = value.ToString();
124 public static int Get(string name, int value) {
125 System.Configuration.KeyValueConfigurationElement element =
126 instance.config.AppSettings.Settings[name];
131 if (int.TryParse(element.Value, out parsedValue))
138 public static void Set(string name, Color color) {
139 instance[name] = color.ToArgb().ToString("X8");
142 public static Color Get(string name, Color value) {
143 System.Configuration.KeyValueConfigurationElement element =
144 instance.config.AppSettings.Settings[name];
149 if (int.TryParse(element.Value,
150 System.Globalization.NumberStyles.HexNumber,
151 System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
152 return Color.FromArgb(parsedValue);
158 public static void Set(string name, float value) {
159 instance[name] = value.ToString(
160 System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
163 public static float Get(string name, float value) {
164 System.Configuration.KeyValueConfigurationElement element =
165 instance.config.AppSettings.Settings[name];
170 if (float.TryParse(element.Value,
171 System.Globalization.NumberStyles.Float,
172 System.Globalization.CultureInfo.InvariantCulture, out parsedValue))