Utilities/Config.cs
author moel.mich
Mon, 05 Apr 2010 15:31:19 +0000
changeset 87 ecdc3bcef083
child 128 cea5477b4d72
permissions -rw-r--r--
Rewritten the T-Balancer code to use the FTDI D2XX drivers directly instead of the System.IO.Ports.SerialPort class. The SerialPort class has some ugly problems like http://connect.microsoft.com/VisualStudio/feedback/details/140018/serialport-crashes-after-disconnect-of-usb-com-port or http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8a1825d2-c84b-4620-91e7-3934a4d47330 for which no real solution seems to exist. And Microsoft doesn't feel like it needs to be fixed for years now.
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@63
    61
    }
moel@63
    62
moel@63
    63
    ~Config() {
moel@63
    64
      string tempName = Path.ChangeExtension(fileName, ".tmp");
moel@63
    65
moel@63
    66
      if (File.Exists(tempName))
moel@63
    67
        File.Delete(tempName);
moel@63
    68
      try {
moel@63
    69
        config.SaveAs(tempName);
moel@63
    70
        if (File.Exists(fileName) && File.Exists(tempName))
moel@63
    71
          File.Delete(fileName);
moel@63
    72
        File.Move(tempName, fileName);
moel@63
    73
      } catch (System.Configuration.ConfigurationErrorsException) { }
moel@63
    74
    }
moel@63
    75
moel@63
    76
    public static Config Settings {
moel@63
    77
      get {
moel@63
    78
        return instance;
moel@63
    79
      }
moel@63
    80
    }
moel@63
    81
moel@63
    82
    public string this[string name] {
moel@63
    83
      get {
moel@63
    84
        System.Configuration.KeyValueConfigurationElement element =
moel@63
    85
          config.AppSettings.Settings[name];
moel@63
    86
        if (element != null)
moel@63
    87
          return element.Value;
moel@63
    88
        else
moel@63
    89
          return null;
moel@63
    90
      }
moel@63
    91
      set {
moel@63
    92
        config.AppSettings.Settings.Remove(name);
moel@63
    93
        config.AppSettings.Settings.Add(name, value);
moel@63
    94
      }
moel@63
    95
    }
moel@63
    96
moel@63
    97
    public static bool Contains(string name) {
moel@63
    98
      System.Configuration.KeyValueConfigurationElement element =
moel@63
    99
        instance.config.AppSettings.Settings[name];
moel@63
   100
      return element != null;
moel@63
   101
    }
moel@63
   102
moel@63
   103
    public static void Remove(string name) {
moel@63
   104
      instance.config.AppSettings.Settings.Remove(name);
moel@63
   105
    }
moel@63
   106
moel@63
   107
    public static void Set(string name, bool value) {
moel@63
   108
      instance[name] = value ? "true" : "false";
moel@63
   109
    }
moel@63
   110
moel@63
   111
    public static bool Get(string name, bool value) {
moel@63
   112
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   113
        instance.config.AppSettings.Settings[name];
moel@63
   114
      if (element == null)
moel@63
   115
        return value;
moel@63
   116
      else
moel@63
   117
        return element.Value == "true"; 
moel@63
   118
    }
moel@63
   119
moel@63
   120
    public static void Set(string name, int value) {
moel@63
   121
      instance[name] = value.ToString();
moel@63
   122
    }
moel@63
   123
moel@63
   124
    public static int Get(string name, int value) {
moel@63
   125
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   126
        instance.config.AppSettings.Settings[name];
moel@63
   127
      if (element == null)
moel@63
   128
        return value;
moel@63
   129
      else {
moel@63
   130
        int parsedValue;
moel@63
   131
        if (int.TryParse(element.Value, out parsedValue))
moel@63
   132
          return parsedValue;
moel@63
   133
        else
moel@63
   134
          return value;
moel@63
   135
      }
moel@63
   136
    }
moel@63
   137
moel@63
   138
    public static void Set(string name, Color color) {
moel@63
   139
      instance[name] = color.ToArgb().ToString("X8");
moel@63
   140
    }
moel@63
   141
moel@63
   142
    public static Color Get(string name, Color value) {
moel@63
   143
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   144
        instance.config.AppSettings.Settings[name];
moel@63
   145
      if (element == null)
moel@63
   146
        return value;
moel@63
   147
      else {
moel@63
   148
        int parsedValue;
moel@63
   149
        if (int.TryParse(element.Value, 
moel@63
   150
          System.Globalization.NumberStyles.HexNumber, 
moel@63
   151
          System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
moel@63
   152
          return Color.FromArgb(parsedValue);
moel@63
   153
        else
moel@63
   154
          return value;
moel@63
   155
      }
moel@63
   156
    }
moel@63
   157
moel@63
   158
    public static void Set(string name, float value) {
moel@63
   159
      instance[name] = value.ToString(
moel@63
   160
        System.Globalization.CultureInfo.InvariantCulture.NumberFormat); 
moel@63
   161
    }
moel@63
   162
moel@63
   163
    public static float Get(string name, float value) {
moel@63
   164
      System.Configuration.KeyValueConfigurationElement element =
moel@63
   165
        instance.config.AppSettings.Settings[name];
moel@63
   166
      if (element == null)
moel@63
   167
        return value;
moel@63
   168
      else {
moel@63
   169
        float parsedValue;
moel@63
   170
        if (float.TryParse(element.Value,
moel@63
   171
          System.Globalization.NumberStyles.Float,
moel@63
   172
          System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
moel@63
   173
          return parsedValue;
moel@63
   174
        else
moel@63
   175
          return value;
moel@63
   176
      }
moel@63
   177
    }
moel@63
   178
  }
moel@63
   179
}