Utilities/Configuration.cs
author moel.mich
Sun, 07 Feb 2010 16:42:26 +0000
changeset 29 f0640baf7128
parent 1 361e324a0ed4
child 42 47385d4fc990
permissions -rw-r--r--
Changed ATI GPU device enumeration.
     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 Remove(string name) {
   103       instance.config.AppSettings.Settings.Remove(name);
   104     }
   105 
   106     public static void Set(string name, bool value) {
   107       instance[name] = value ? "true" : "false";
   108     }
   109 
   110     public static bool Get(string name, bool value) {
   111       System.Configuration.KeyValueConfigurationElement element =
   112         instance.config.AppSettings.Settings[name];
   113       if (element == null)
   114         return value;
   115       else
   116         return element.Value == "true"; 
   117     }
   118 
   119     public static void Set(string name, int value) {
   120       instance[name] = value.ToString();
   121     }
   122 
   123     public static int Get(string name, int value) {
   124       System.Configuration.KeyValueConfigurationElement element =
   125         instance.config.AppSettings.Settings[name];
   126       if (element == null)
   127         return value;
   128       else {
   129         int parsedValue;
   130         if (int.TryParse(element.Value, out parsedValue))
   131           return parsedValue;
   132         else
   133           return value;
   134       }
   135     }
   136   }
   137 }