Utilities/Config.cs
author moel.mich
Thu, 05 Aug 2010 19:28:50 +0000
changeset 164 cc1e116d0f2c
parent 128 cea5477b4d72
permissions -rw-r--r--
Added a fan control sensor for ATI GPUs.
moel@63
     1
/*
moel@63
     2
  
moel@63
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@63
     4
moel@63
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@63
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@63
     7
  the License. You may obtain a copy of the License at
moel@63
     8
 
moel@63
     9
  http://www.mozilla.org/MPL/
moel@63
    10
moel@63
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@63
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@63
    13
  for the specific language governing rights and limitations under the License.
moel@63
    14
moel@63
    15
  The Original Code is the Open Hardware Monitor code.
moel@63
    16
moel@63
    17
  The Initial Developer of the Original Code is 
moel@63
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@63
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@63
    20
  the Initial Developer. All Rights Reserved.
moel@63
    21
moel@63
    22
  Contributor(s):
moel@63
    23
moel@63
    24
  Alternatively, the contents of this file may be used under the terms of
moel@63
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@63
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@63
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@63
    28
  of those above. If you wish to allow use of your version of this file only
moel@63
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@63
    30
  use your version of this file under the terms of the MPL, indicate your
moel@63
    31
  decision by deleting the provisions above and replace them with the notice
moel@63
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@63
    33
  the provisions above, a recipient may use your version of this file under
moel@63
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@63
    35
 
moel@63
    36
*/
moel@63
    37
moel@63
    38
using System;
moel@63
    39
using System.Collections.Generic;
moel@63
    40
using System.Drawing;
moel@63
    41
using System.IO;
moel@63
    42
moel@63
    43
namespace OpenHardwareMonitor.Utilities {
moel@63
    44
moel@63
    45
  public sealed class Config {
moel@63
    46
    private static readonly Config instance = new Config();
moel@63
    47
moel@63
    48
    private string fileName;
moel@63
    49
moel@63
    50
    private System.Configuration.Configuration config;
moel@63
    51
moel@63
    52
    private Config() {
moel@63
    53
      this.fileName = Path.ChangeExtension(
moel@63
    54
        System.Windows.Forms.Application.ExecutablePath, ".config");
moel@63
    55
      System.Configuration.ExeConfigurationFileMap fileMap = 
moel@63
    56
        new System.Configuration.ExeConfigurationFileMap();
moel@63
    57
      fileMap.ExeConfigFilename = fileName;        
moel@63
    58
      config = System.Configuration.ConfigurationManager.
moel@63
    59
        OpenMappedExeConfiguration(fileMap, 
moel@63
    60
        System.Configuration.ConfigurationUserLevel.None);
moel@157
    61
      try {
moel@157
    62
        // try to load the settings
moel@157
    63
        System.Configuration.KeyValueConfigurationCollection collection =
moel@157
    64
          config.AppSettings.Settings;        
moel@157
    65
      } catch {
moel@157
    66
        // if an exception is thrown, start with a new config file
moel@157
    67
        if (File.Exists(fileName))
moel@157
    68
          File.Delete(fileName);
moel@157
    69
        config = System.Configuration.ConfigurationManager.
moel@157
    70
          OpenMappedExeConfiguration(fileMap,
moel@157
    71
          System.Configuration.ConfigurationUserLevel.None);
moel@157
    72
      }
moel@63
    73
    }
moel@63
    74
moel@128
    75
    private void SaveConfig() {
moel@63
    76
      string tempName = Path.ChangeExtension(fileName, ".tmp");
moel@63
    77
moel@63
    78
      if (File.Exists(tempName))
moel@63
    79
        File.Delete(tempName);
moel@63
    80
      try {
moel@63
    81
        config.SaveAs(tempName);
moel@63
    82
        if (File.Exists(fileName) && File.Exists(tempName))
moel@63
    83
          File.Delete(fileName);
moel@63
    84
        File.Move(tempName, fileName);
moel@63
    85
      } catch (System.Configuration.ConfigurationErrorsException) { }
moel@63
    86
    }
moel@63
    87
moel@128
    88
    public static void Save() {
moel@128
    89
      instance.SaveConfig();
moel@128
    90
    }
moel@128
    91
moel@63
    92
    public static Config Settings {
moel@63
    93
      get {
moel@63
    94
        return instance;
moel@63
    95
      }
moel@63
    96
    }
moel@63
    97
moel@63
    98
    public string this[string name] {
moel@63
    99
      get {
moel@63
   100
        System.Configuration.KeyValueConfigurationElement element =
moel@63
   101
          config.AppSettings.Settings[name];
moel@63
   102
        if (element != null)
moel@63
   103
          return element.Value;
moel@63
   104
        else
moel@63
   105
          return null;
moel@63
   106
      }
moel@63
   107
      set {
moel@63
   108
        config.AppSettings.Settings.Remove(name);
moel@63
   109
        config.AppSettings.Settings.Add(name, value);
moel@63
   110
      }
moel@63
   111
    }
moel@63
   112
moel@63
   113
    public static bool Contains(string name) {
moel@63
   114
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   115
        instance.config.AppSettings.Settings[name];
moel@63
   116
      return element != null;
moel@63
   117
    }
moel@63
   118
moel@63
   119
    public static void Remove(string name) {
moel@63
   120
      instance.config.AppSettings.Settings.Remove(name);
moel@63
   121
    }
moel@63
   122
moel@63
   123
    public static void Set(string name, bool value) {
moel@63
   124
      instance[name] = value ? "true" : "false";
moel@63
   125
    }
moel@63
   126
moel@63
   127
    public static bool Get(string name, bool value) {
moel@63
   128
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   129
        instance.config.AppSettings.Settings[name];
moel@63
   130
      if (element == null)
moel@63
   131
        return value;
moel@63
   132
      else
moel@63
   133
        return element.Value == "true"; 
moel@63
   134
    }
moel@63
   135
moel@63
   136
    public static void Set(string name, int value) {
moel@63
   137
      instance[name] = value.ToString();
moel@63
   138
    }
moel@63
   139
moel@63
   140
    public static int Get(string name, int value) {
moel@63
   141
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   142
        instance.config.AppSettings.Settings[name];
moel@63
   143
      if (element == null)
moel@63
   144
        return value;
moel@63
   145
      else {
moel@63
   146
        int parsedValue;
moel@63
   147
        if (int.TryParse(element.Value, out parsedValue))
moel@63
   148
          return parsedValue;
moel@63
   149
        else
moel@63
   150
          return value;
moel@63
   151
      }
moel@63
   152
    }
moel@63
   153
moel@63
   154
    public static void Set(string name, Color color) {
moel@63
   155
      instance[name] = color.ToArgb().ToString("X8");
moel@63
   156
    }
moel@63
   157
moel@63
   158
    public static Color Get(string name, Color value) {
moel@63
   159
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   160
        instance.config.AppSettings.Settings[name];
moel@63
   161
      if (element == null)
moel@63
   162
        return value;
moel@63
   163
      else {
moel@63
   164
        int parsedValue;
moel@63
   165
        if (int.TryParse(element.Value, 
moel@63
   166
          System.Globalization.NumberStyles.HexNumber, 
moel@63
   167
          System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
moel@63
   168
          return Color.FromArgb(parsedValue);
moel@63
   169
        else
moel@63
   170
          return value;
moel@63
   171
      }
moel@63
   172
    }
moel@63
   173
moel@63
   174
    public static void Set(string name, float value) {
moel@63
   175
      instance[name] = value.ToString(
moel@63
   176
        System.Globalization.CultureInfo.InvariantCulture.NumberFormat); 
moel@63
   177
    }
moel@63
   178
moel@63
   179
    public static float Get(string name, float value) {
moel@63
   180
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   181
        instance.config.AppSettings.Settings[name];
moel@63
   182
      if (element == null)
moel@63
   183
        return value;
moel@63
   184
      else {
moel@63
   185
        float parsedValue;
moel@63
   186
        if (float.TryParse(element.Value,
moel@63
   187
          System.Globalization.NumberStyles.Float,
moel@63
   188
          System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
moel@63
   189
          return parsedValue;
moel@63
   190
        else
moel@63
   191
          return value;
moel@63
   192
      }
moel@63
   193
    }
moel@63
   194
  }
moel@63
   195
}