Utilities/Configuration.cs
author moel.mich
Tue, 26 Jan 2010 22:37:48 +0000
changeset 1 361e324a0ed4
child 28 9b205b2ab056
permissions -rw-r--r--
Initial commit.
     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.IO;
    41 
    42 namespace OpenHardwareMonitor.Utilities {
    43 
    44   public sealed class Config {
    45     private static readonly Config instance = new Config();
    46 
    47     private string fileName;
    48 
    49     private System.Configuration.Configuration config;
    50 
    51     private Config() {
    52       this.fileName = Path.ChangeExtension(
    53         System.Windows.Forms.Application.ExecutablePath, ".config");
    54       System.Configuration.ExeConfigurationFileMap fileMap = 
    55         new System.Configuration.ExeConfigurationFileMap();
    56       fileMap.ExeConfigFilename = fileName;        
    57       config = System.Configuration.ConfigurationManager.
    58         OpenMappedExeConfiguration(fileMap, 
    59         System.Configuration.ConfigurationUserLevel.None);
    60     }
    61 
    62     ~Config() {
    63       string tempName = Path.ChangeExtension(fileName, ".tmp");
    64 
    65       if (File.Exists(tempName))
    66         File.Delete(tempName);
    67       try {
    68         config.SaveAs(tempName);
    69         if (File.Exists(fileName) && File.Exists(tempName))
    70           File.Delete(fileName);
    71         File.Move(tempName, fileName);
    72       } catch (System.Configuration.ConfigurationErrorsException) { }
    73     }
    74 
    75     public static Config Settings {
    76       get {
    77         return instance;
    78       }
    79     }
    80 
    81     public string this[string name] {
    82       get {
    83         System.Configuration.KeyValueConfigurationElement element =
    84           config.AppSettings.Settings[name];
    85         if (element != null)
    86           return element.Value;
    87         else
    88           return null;
    89       }
    90       set {
    91         config.AppSettings.Settings.Remove(name);
    92         config.AppSettings.Settings.Add(name, value);
    93       }
    94     }
    95 
    96     public static bool Contains(string name) {
    97       System.Configuration.KeyValueConfigurationElement element =
    98         instance.config.AppSettings.Settings[name];
    99       return element != null;
   100     }
   101 
   102     public static void Set(string name, bool value) {
   103       instance[name] = value ? "true" : "false";
   104     }
   105 
   106     public static bool Get(string name, bool value) {
   107       System.Configuration.KeyValueConfigurationElement element =
   108         instance.config.AppSettings.Settings[name];
   109       if (element == null)
   110         return value;
   111       else
   112         return element.Value == "true"; 
   113     }
   114 
   115     public static void Set(string name, int value) {
   116       instance[name] = value.ToString();
   117     }
   118 
   119     public static int Get(string name, int value) {
   120       System.Configuration.KeyValueConfigurationElement element =
   121         instance.config.AppSettings.Settings[name];
   122       if (element == null)
   123         return value;
   124       else {
   125         int parsedValue;
   126         if (int.TryParse(element.Value, out parsedValue))
   127           return parsedValue;
   128         else
   129           return value;
   130       }
   131     }
   132   }
   133 }