Utilities/Config.cs
changeset 165 813d8bc3192f
parent 164 cc1e116d0f2c
child 166 fa9dfbfc4145
     1.1 --- a/Utilities/Config.cs	Thu Aug 05 19:28:50 2010 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,195 +0,0 @@
     1.4 -/*
     1.5 -  
     1.6 -  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 -
     1.8 -  The contents of this file are subject to the Mozilla Public License Version
     1.9 -  1.1 (the "License"); you may not use this file except in compliance with
    1.10 -  the License. You may obtain a copy of the License at
    1.11 - 
    1.12 -  http://www.mozilla.org/MPL/
    1.13 -
    1.14 -  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 -  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 -  for the specific language governing rights and limitations under the License.
    1.17 -
    1.18 -  The Original Code is the Open Hardware Monitor code.
    1.19 -
    1.20 -  The Initial Developer of the Original Code is 
    1.21 -  Michael Möller <m.moeller@gmx.ch>.
    1.22 -  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 -  the Initial Developer. All Rights Reserved.
    1.24 -
    1.25 -  Contributor(s):
    1.26 -
    1.27 -  Alternatively, the contents of this file may be used under the terms of
    1.28 -  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 -  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 -  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 -  of those above. If you wish to allow use of your version of this file only
    1.32 -  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 -  use your version of this file under the terms of the MPL, indicate your
    1.34 -  decision by deleting the provisions above and replace them with the notice
    1.35 -  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 -  the provisions above, a recipient may use your version of this file under
    1.37 -  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 - 
    1.39 -*/
    1.40 -
    1.41 -using System;
    1.42 -using System.Collections.Generic;
    1.43 -using System.Drawing;
    1.44 -using System.IO;
    1.45 -
    1.46 -namespace OpenHardwareMonitor.Utilities {
    1.47 -
    1.48 -  public sealed class Config {
    1.49 -    private static readonly Config instance = new Config();
    1.50 -
    1.51 -    private string fileName;
    1.52 -
    1.53 -    private System.Configuration.Configuration config;
    1.54 -
    1.55 -    private Config() {
    1.56 -      this.fileName = Path.ChangeExtension(
    1.57 -        System.Windows.Forms.Application.ExecutablePath, ".config");
    1.58 -      System.Configuration.ExeConfigurationFileMap fileMap = 
    1.59 -        new System.Configuration.ExeConfigurationFileMap();
    1.60 -      fileMap.ExeConfigFilename = fileName;        
    1.61 -      config = System.Configuration.ConfigurationManager.
    1.62 -        OpenMappedExeConfiguration(fileMap, 
    1.63 -        System.Configuration.ConfigurationUserLevel.None);
    1.64 -      try {
    1.65 -        // try to load the settings
    1.66 -        System.Configuration.KeyValueConfigurationCollection collection =
    1.67 -          config.AppSettings.Settings;        
    1.68 -      } catch {
    1.69 -        // if an exception is thrown, start with a new config file
    1.70 -        if (File.Exists(fileName))
    1.71 -          File.Delete(fileName);
    1.72 -        config = System.Configuration.ConfigurationManager.
    1.73 -          OpenMappedExeConfiguration(fileMap,
    1.74 -          System.Configuration.ConfigurationUserLevel.None);
    1.75 -      }
    1.76 -    }
    1.77 -
    1.78 -    private void SaveConfig() {
    1.79 -      string tempName = Path.ChangeExtension(fileName, ".tmp");
    1.80 -
    1.81 -      if (File.Exists(tempName))
    1.82 -        File.Delete(tempName);
    1.83 -      try {
    1.84 -        config.SaveAs(tempName);
    1.85 -        if (File.Exists(fileName) && File.Exists(tempName))
    1.86 -          File.Delete(fileName);
    1.87 -        File.Move(tempName, fileName);
    1.88 -      } catch (System.Configuration.ConfigurationErrorsException) { }
    1.89 -    }
    1.90 -
    1.91 -    public static void Save() {
    1.92 -      instance.SaveConfig();
    1.93 -    }
    1.94 -
    1.95 -    public static Config Settings {
    1.96 -      get {
    1.97 -        return instance;
    1.98 -      }
    1.99 -    }
   1.100 -
   1.101 -    public string this[string name] {
   1.102 -      get {
   1.103 -        System.Configuration.KeyValueConfigurationElement element =
   1.104 -          config.AppSettings.Settings[name];
   1.105 -        if (element != null)
   1.106 -          return element.Value;
   1.107 -        else
   1.108 -          return null;
   1.109 -      }
   1.110 -      set {
   1.111 -        config.AppSettings.Settings.Remove(name);
   1.112 -        config.AppSettings.Settings.Add(name, value);
   1.113 -      }
   1.114 -    }
   1.115 -
   1.116 -    public static bool Contains(string name) {
   1.117 -      System.Configuration.KeyValueConfigurationElement element =
   1.118 -        instance.config.AppSettings.Settings[name];
   1.119 -      return element != null;
   1.120 -    }
   1.121 -
   1.122 -    public static void Remove(string name) {
   1.123 -      instance.config.AppSettings.Settings.Remove(name);
   1.124 -    }
   1.125 -
   1.126 -    public static void Set(string name, bool value) {
   1.127 -      instance[name] = value ? "true" : "false";
   1.128 -    }
   1.129 -
   1.130 -    public static bool Get(string name, bool value) {
   1.131 -      System.Configuration.KeyValueConfigurationElement element =
   1.132 -        instance.config.AppSettings.Settings[name];
   1.133 -      if (element == null)
   1.134 -        return value;
   1.135 -      else
   1.136 -        return element.Value == "true"; 
   1.137 -    }
   1.138 -
   1.139 -    public static void Set(string name, int value) {
   1.140 -      instance[name] = value.ToString();
   1.141 -    }
   1.142 -
   1.143 -    public static int Get(string name, int value) {
   1.144 -      System.Configuration.KeyValueConfigurationElement element =
   1.145 -        instance.config.AppSettings.Settings[name];
   1.146 -      if (element == null)
   1.147 -        return value;
   1.148 -      else {
   1.149 -        int parsedValue;
   1.150 -        if (int.TryParse(element.Value, out parsedValue))
   1.151 -          return parsedValue;
   1.152 -        else
   1.153 -          return value;
   1.154 -      }
   1.155 -    }
   1.156 -
   1.157 -    public static void Set(string name, Color color) {
   1.158 -      instance[name] = color.ToArgb().ToString("X8");
   1.159 -    }
   1.160 -
   1.161 -    public static Color Get(string name, Color value) {
   1.162 -      System.Configuration.KeyValueConfigurationElement element =
   1.163 -        instance.config.AppSettings.Settings[name];
   1.164 -      if (element == null)
   1.165 -        return value;
   1.166 -      else {
   1.167 -        int parsedValue;
   1.168 -        if (int.TryParse(element.Value, 
   1.169 -          System.Globalization.NumberStyles.HexNumber, 
   1.170 -          System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   1.171 -          return Color.FromArgb(parsedValue);
   1.172 -        else
   1.173 -          return value;
   1.174 -      }
   1.175 -    }
   1.176 -
   1.177 -    public static void Set(string name, float value) {
   1.178 -      instance[name] = value.ToString(
   1.179 -        System.Globalization.CultureInfo.InvariantCulture.NumberFormat); 
   1.180 -    }
   1.181 -
   1.182 -    public static float Get(string name, float value) {
   1.183 -      System.Configuration.KeyValueConfigurationElement element =
   1.184 -        instance.config.AppSettings.Settings[name];
   1.185 -      if (element == null)
   1.186 -        return value;
   1.187 -      else {
   1.188 -        float parsedValue;
   1.189 -        if (float.TryParse(element.Value,
   1.190 -          System.Globalization.NumberStyles.Float,
   1.191 -          System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   1.192 -          return parsedValue;
   1.193 -        else
   1.194 -          return value;
   1.195 -      }
   1.196 -    }
   1.197 -  }
   1.198 -}