Utilities/Config.cs
author moel.mich
Sat, 24 Apr 2010 20:09:18 +0000
changeset 91 c0937b698b81
child 128 cea5477b4d72
permissions -rw-r--r--
Fixed Issue 39.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 using System.IO;
    42 
    43 namespace OpenHardwareMonitor.Utilities {
    44 
    45   public sealed class Config {
    46     private static readonly Config instance = new Config();
    47 
    48     private string fileName;
    49 
    50     private System.Configuration.Configuration config;
    51 
    52     private 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);
    61     }
    62 
    63     ~Config() {
    64       string tempName = Path.ChangeExtension(fileName, ".tmp");
    65 
    66       if (File.Exists(tempName))
    67         File.Delete(tempName);
    68       try {
    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) { }
    74     }
    75 
    76     public static Config Settings {
    77       get {
    78         return instance;
    79       }
    80     }
    81 
    82     public string this[string name] {
    83       get {
    84         System.Configuration.KeyValueConfigurationElement element =
    85           config.AppSettings.Settings[name];
    86         if (element != null)
    87           return element.Value;
    88         else
    89           return null;
    90       }
    91       set {
    92         config.AppSettings.Settings.Remove(name);
    93         config.AppSettings.Settings.Add(name, value);
    94       }
    95     }
    96 
    97     public static bool Contains(string name) {
    98       System.Configuration.KeyValueConfigurationElement element =
    99         instance.config.AppSettings.Settings[name];
   100       return element != null;
   101     }
   102 
   103     public static void Remove(string name) {
   104       instance.config.AppSettings.Settings.Remove(name);
   105     }
   106 
   107     public static void Set(string name, bool value) {
   108       instance[name] = value ? "true" : "false";
   109     }
   110 
   111     public static bool Get(string name, bool value) {
   112       System.Configuration.KeyValueConfigurationElement element =
   113         instance.config.AppSettings.Settings[name];
   114       if (element == null)
   115         return value;
   116       else
   117         return element.Value == "true"; 
   118     }
   119 
   120     public static void Set(string name, int value) {
   121       instance[name] = value.ToString();
   122     }
   123 
   124     public static int Get(string name, int value) {
   125       System.Configuration.KeyValueConfigurationElement element =
   126         instance.config.AppSettings.Settings[name];
   127       if (element == null)
   128         return value;
   129       else {
   130         int parsedValue;
   131         if (int.TryParse(element.Value, out parsedValue))
   132           return parsedValue;
   133         else
   134           return value;
   135       }
   136     }
   137 
   138     public static void Set(string name, Color color) {
   139       instance[name] = color.ToArgb().ToString("X8");
   140     }
   141 
   142     public static Color Get(string name, Color value) {
   143       System.Configuration.KeyValueConfigurationElement element =
   144         instance.config.AppSettings.Settings[name];
   145       if (element == null)
   146         return value;
   147       else {
   148         int parsedValue;
   149         if (int.TryParse(element.Value, 
   150           System.Globalization.NumberStyles.HexNumber, 
   151           System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   152           return Color.FromArgb(parsedValue);
   153         else
   154           return value;
   155       }
   156     }
   157 
   158     public static void Set(string name, float value) {
   159       instance[name] = value.ToString(
   160         System.Globalization.CultureInfo.InvariantCulture.NumberFormat); 
   161     }
   162 
   163     public static float Get(string name, float value) {
   164       System.Configuration.KeyValueConfigurationElement element =
   165         instance.config.AppSettings.Settings[name];
   166       if (element == null)
   167         return value;
   168       else {
   169         float parsedValue;
   170         if (float.TryParse(element.Value,
   171           System.Globalization.NumberStyles.Float,
   172           System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   173           return parsedValue;
   174         else
   175           return value;
   176       }
   177     }
   178   }
   179 }