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
}
}
|