RegistryConfiguration.cs :  » Development » devAdvantage » AnticipatingMinds » PlatformServices » Configuration » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » devAdvantage 
devAdvantage » AnticipatingMinds » PlatformServices » Configuration » RegistryConfiguration.cs
using System;
using System.IO;
using System.Xml;
using System.Diagnostics;
using Microsoft.Win32;

namespace AnticipatingMinds.PlatformServices.Configuration{
  /// <summary>
  /// Represents an accessor to configurtion values stored in registry.
  /// </summary>
  public class RegistryConfiguration
  {
    #region Nested Types
    /// <summary>
    /// Indicates targeted registry brunch.
    /// </summary>
    public enum RegistryTarget 
    {
      /// <summary>
      /// Indicates HKEY_CURRENT_USER registry brunch
      /// </summary>
      CurrentUser, 
      /// <summary>
      /// Indicates HKEY_LOCAL_MACHINE registry brunch
      /// </summary>
      LocalMachine
    };
    #endregion Nested Types
    #region Instance constructors
    /// <summary>
    /// Initializes new instance of <see cref="RegistryConfiguration"/> class.
    /// </summary>
    /// <param name="applicationRoot">Root of configuration values.</param>
    /// <param name="target">Targeted registry brunch.</param>
    public RegistryConfiguration(string applicationRoot,RegistryTarget target)
    {
      this.applicationRoot = applicationRoot;
      this.target = target;
    }
    #endregion Instance constructors
    #region Public instance methods
    /// <summary>
    /// Gets a sring value from registry.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <returns>Registry value if found or String.Empty otherwise.</returns>
    public string GetString(string path, string valueName)
    {
      return GetString(path,valueName,string.Empty);
    }
    /// <summary>
    /// Gets a sring value from registry.
    /// </summary>
    /// <param name="valueName">Value name.</param>
    /// <returns>Registry value if found or String.Empty otherwise.</returns>
    public string GetString(string valueName)
    {
      return GetString(string.Empty,valueName);
    }
    /// <summary>
    /// Gets a sring value from registry.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <param name="defaultValue">Value to return if regustry value does not exist.</param>
    /// <returns>Registry value if found or defaultValue otherwise.</returns>
    public string GetString(string path, string valueName,string defaultValue)
    {
      using(RegistryKey key = GetRegistryKey(path))
      {
        if(key == null)
          return defaultValue;
        object value = key.GetValue(valueName,defaultValue);
        if(value is string)
          return (string) value;
        else
          return defaultValue;
      }
    }
    /// <summary>
    /// Gets a sring array from registry.
    /// </summary>
    /// <param name="valueName">Value name.</param>
    /// <returns>Registry value if found or String[0] otherwise.</returns>
    public string[] GetMultiString(string valueName)
    {
      return GetMultiString(string.Empty,valueName,emptyStringArray);
    }
    /// <summary>
    /// Gets a sring array from registry.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <returns>Registry value if found or String[0] otherwise.</returns>
    public string[] GetMultiString(string path, string valueName)
    {
      return GetMultiString(path,valueName,emptyStringArray);
    }
    /// <summary>
    /// Gets a sring array from registry.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <param name="defaultValue">Value to return if regustry value does not exist.</param>
    /// <returns>Registry value if found or String[0] otherwise.</returns>
    public string[] GetMultiString(string path, string valueName,string[] defaultValue)
    {
      using(RegistryKey key = GetRegistryKey(path))
      {
        if(key == null)
          return defaultValue;
        object value = key.GetValue(valueName,defaultValue);
        if(value is string[])
          return (string[]) value;
        else
          return defaultValue;
      }
    }
    /// <summary>
    /// Gets an integer value from registry.
    /// </summary>
    /// <param name="valueName">Value name.</param>
    /// <returns>Registry value if found or 0 otherwise.</returns>
    public Int32 GetInt32(string valueName)
    {
      return GetInt32(string.Empty,valueName);
    }
    /// <summary>
    /// Gets an integer value from registry.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <returns>Registry value if found or 0 otherwise.</returns>
    public Int32 GetInt32(string path, string valueName)
    {
      return GetInt32(path,valueName,0);
    }
    /// <summary>
    /// Gets an integer value from registry.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <param name="defaultValue">Value to return if regustry value does not exist.</param>
    /// <returns>Registry value if found or 0 otherwise.</returns>
    public Int32 GetInt32(string path, string valueName,Int32 defaultValue)
    {
      using(RegistryKey key = GetRegistryKey(path))
      {
        if(key == null)
          return defaultValue;
        object value = key.GetValue(valueName,defaultValue);
        if(value is Int32)
          return (Int32) value;
        else
          return defaultValue;
      }
    }
    /// <summary>
    /// Get an Xml representation of registry brunch and its descendants.
    /// </summary>
    /// <param name="path">Path to the targeted brunch, relative to applicationRoot.</param>
    /// <returns></returns>
    public XmlDocument GetTree(string path)
    {
      using(RegistryKey key = GetRegistryKey(path))
      {
        XmlDocument registryXml = new XmlDocument();
        XmlElement rootElement = GetRegistryKeyXmlElement(key,"root",registryXml);
        if(rootElement != null)
          registryXml.AppendChild(rootElement);
        return registryXml;
      }
    }
    /// <summary>
    /// Sets registry string value.
    /// </summary>
    /// <param name="valueName">Value name.</param>
    /// <param name="value">Value to set.</param>
    public void SetValue(string valueName,string value)
    {
      SetValue(string.Empty,valueName,(object) value);
    }
    /// <summary>
    /// Sets registry string value.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <param name="value">Value to set.</param>
    public void SetValue(string path,string valueName,string value)
    {
      SetValue(path,valueName,(object) value);
    }

    /// <summary>
    /// Sets registry string array value.
    /// </summary>
    /// <param name="valueName">Value name.</param>
    /// <param name="value">Value to set.</param>
    public void SetValue(string valueName,string[] value)
    {
      SetValue(string.Empty,valueName,(object) value);
    }

    /// <summary>
    /// Sets registry string array value.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <param name="value">Value to set.</param>
    public void SetValue(string path,string valueName,string[] value)
    {
      SetValue(path,valueName,(object) value);
    }
    /// <summary>
    /// Sets registry integer value.
    /// </summary>
    /// <param name="valueName">Value name.</param>
    /// <param name="value">Value to set.</param>
    public void SetValue(string valueName,Int32 value)
    {
      SetValue(string.Empty,valueName,(object) value);
    }
    /// <summary>
    /// Sets registry integer value.
    /// </summary>
    /// <param name="path">Path to the value, relative to applicationRoot.</param>
    /// <param name="valueName">Value name.</param>
    /// <param name="value">Value to set.</param>
    public void SetValue(string path,string valueName,Int32 value)
    {
      SetValue(path,valueName,(object) value);
    }
    #endregion Public instance methods
    #region Private instance methods
    private void SetValue(string path,string valueName,object value)
    {
      using(RegistryKey createdKey = GetTargetKey().CreateSubKey(GetFullKeyPath(path)))
      {
        if(createdKey == null)
          return;
        createdKey.SetValue(valueName,value);
      }
    }
    private XmlElement GetRegistryKeyXmlElement(RegistryKey key,string keyName,XmlDocument registryXml)
    {
      if(key == null)
        return null;
      XmlElement keyElement = registryXml.CreateElement(keyName);
      string[] subKeyNames = key.GetSubKeyNames();
      string[] valueNames = key.GetValueNames();
      foreach(string subkeyName in subKeyNames)
      {
        using(RegistryKey subKey = key.OpenSubKey(subkeyName))
        {
          XmlElement subkeyElement = GetRegistryKeyXmlElement(subKey,subkeyName,registryXml);
          if(subkeyElement != null)
          {
            subkeyElement.SetAttribute("isKey","true");
            keyElement.AppendChild(subkeyElement);
          }
        }
      }
      foreach(string valueName in valueNames)
      {
        object value = key.GetValue(valueName);
        if(value is Array)
        {
          Array valueItems = (Array) value;
          foreach(object valueItem in valueItems)
          {
            XmlElement valueElement = registryXml.CreateElement(valueName);
            valueElement.SetAttribute("isKey","false");
            valueElement.InnerText = valueItem.ToString();
            keyElement.AppendChild(valueElement);
          }
        }
        else
        {
          XmlElement valueElement = registryXml.CreateElement(valueName);
          valueElement.SetAttribute("isKey","false");
          valueElement.InnerText = value.ToString();
          keyElement.AppendChild(valueElement);
        }
      }
      return keyElement;
    }
    private RegistryKey GetTargetKey()
    {
      switch(target)
      {
        case RegistryTarget.CurrentUser:
        {
          return Registry.CurrentUser;
        }
        case RegistryTarget.LocalMachine:
        {
          return Registry.LocalMachine;
        }
        default:
        {
          Debug.Assert(false,"If you have added targts, please add corresponding case above");
          return null;
        }
      }
    }
    private string GetFullKeyPath(string path)
    {
      if(applicationRoot.Length != 0)
      {
        if(path.Length != 0 && path[0] == '\\')
          return  applicationRoot + path;
        else
          return  applicationRoot + "\\" + path;
      }
      else
      {
        return path;
      }
    }
    private RegistryKey GetRegistryKey(string path)
    {
      string fullPath = GetFullKeyPath(path);
      using(RegistryKey targetKey = GetTargetKey())
      {
        if(targetKey != null)
          return targetKey.OpenSubKey(fullPath,false);
        else
          return null;
      }
    }
    #endregion Private instance methods
    #region Private instance fields
    private static string[] emptyStringArray = new string[0];
    private string applicationRoot;
    private RegistryTarget target;
    #endregion Private instance fields
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.