2014-07-19 11:04:19 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
|
|
|
|
Copyright (C) 2002-2014 Nuclex Development Labs
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
|
License along with this library
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
2014-07-22 08:50:05 +00:00
|
|
|
|
#if WINDOWS
|
|
|
|
|
|
2014-07-19 11:04:19 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-07-22 11:10:14 +00:00
|
|
|
|
using System.Globalization;
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
2014-07-22 10:43:23 +00:00
|
|
|
|
using Microsoft.Win32;
|
2014-07-22 11:10:14 +00:00
|
|
|
|
|
2014-07-22 10:43:23 +00:00
|
|
|
|
using Nuclex.Support.Parsing;
|
|
|
|
|
|
2014-07-19 22:16:41 +00:00
|
|
|
|
namespace Nuclex.Support.Settings {
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
2014-07-19 22:16:41 +00:00
|
|
|
|
/// <summary>Stores settings in the registry on Windows operating systems</summary>
|
|
|
|
|
public class WindowsRegistryStore : ISettingsStore, IDisposable {
|
|
|
|
|
|
2014-07-22 10:43:23 +00:00
|
|
|
|
/// <summary>Initializes a new settings store on the specified registry path</summary>
|
|
|
|
|
/// <param name="hive">Hive in which to look</param>
|
|
|
|
|
/// <param name="directory">Base path of the settings in the specified hive</param>
|
|
|
|
|
/// <param name="writable">Whether to open the registry in writable mode</param>
|
|
|
|
|
public WindowsRegistryStore(RegistryHive hive, string directory, bool writable = true) {
|
|
|
|
|
using(RegistryKey hiveKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default)) {
|
|
|
|
|
this.rootKey = hiveKey.OpenSubKey(directory, writable);
|
|
|
|
|
}
|
|
|
|
|
this.writable = writable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new settings store on the specified registry key</summary>
|
|
|
|
|
/// <param name="rootKey">Registry key the settings are stored under</param>
|
|
|
|
|
/// <param name="writable">Whether the registry was opened in writable mode</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This constructor takes ownership of the registry key. It will be disposed when
|
|
|
|
|
/// the settings store is disposed.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public WindowsRegistryStore(RegistryKey rootKey, bool writable = true) {
|
|
|
|
|
this.rootKey = rootKey;
|
|
|
|
|
this.writable = writable;
|
|
|
|
|
}
|
2014-07-22 08:50:05 +00:00
|
|
|
|
|
2014-07-19 22:16:41 +00:00
|
|
|
|
/// <summary>Immediately releases all resources owned by the instance</summary>
|
|
|
|
|
public void Dispose() {
|
2014-07-22 10:43:23 +00:00
|
|
|
|
if(this.rootKey != null) {
|
|
|
|
|
this.rootKey.Dispose();
|
|
|
|
|
this.rootKey = null;
|
|
|
|
|
}
|
2014-07-19 22:16:41 +00:00
|
|
|
|
}
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Enumerates the categories defined in the configuration</summary>
|
|
|
|
|
/// <returns>An enumerable list of all used categories</returns>
|
2014-07-19 22:16:41 +00:00
|
|
|
|
public IEnumerable<string> EnumerateCategories() {
|
2014-07-22 10:43:23 +00:00
|
|
|
|
return this.rootKey.GetSubKeyNames();
|
2014-07-19 22:16:41 +00:00
|
|
|
|
}
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Enumerates the options stored under the specified category</summary>
|
|
|
|
|
/// <param name="category">Category whose options will be enumerated</param>
|
|
|
|
|
/// <returns>An enumerable list of all options in the category</returns>
|
2014-07-19 22:16:41 +00:00
|
|
|
|
public IEnumerable<OptionInfo> EnumerateOptions(string category = null) {
|
2014-07-22 10:43:23 +00:00
|
|
|
|
if(string.IsNullOrEmpty(category)) {
|
|
|
|
|
string[] valueNames = this.rootKey.GetValueNames();
|
|
|
|
|
for(int index = 0; index < valueNames.Length; ++index) {
|
|
|
|
|
yield return new OptionInfo() {
|
|
|
|
|
Name = valueNames[index],
|
|
|
|
|
OptionType = getBestMatchingType(this.rootKey, valueNames[index])
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
using(RegistryKey categoryKey = this.rootKey.OpenSubKey(category, this.writable)) {
|
|
|
|
|
string[] valueNames = categoryKey.GetValueNames();
|
|
|
|
|
for(int index = 0; index < valueNames.Length; ++index) {
|
|
|
|
|
yield return new OptionInfo() {
|
|
|
|
|
Name = valueNames[index],
|
|
|
|
|
OptionType = getBestMatchingType(categoryKey, valueNames[index])
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-19 22:16:41 +00:00
|
|
|
|
}
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Retrieves the value of the specified option</summary>
|
|
|
|
|
/// <typeparam name="TValue">Type the option will be converted to</typeparam>
|
|
|
|
|
/// <param name="category">Category the option can be found in. Can be null.</param>
|
|
|
|
|
/// <param name="optionName">Name of the option that will be looked up</param>
|
|
|
|
|
/// <returns>The value of the option with the specified name</returns>
|
2014-07-19 22:16:41 +00:00
|
|
|
|
public TValue Get<TValue>(string category, string optionName) {
|
|
|
|
|
TValue value;
|
|
|
|
|
if(TryGet<TValue>(category, optionName, out value)) {
|
|
|
|
|
return value;
|
|
|
|
|
} else {
|
|
|
|
|
if(string.IsNullOrEmpty(category)) {
|
|
|
|
|
throw new KeyNotFoundException(
|
|
|
|
|
"There is no option named '" + optionName + "' in the registry"
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
throw new KeyNotFoundException(
|
|
|
|
|
"There is no option named '" + optionName + "' under the category '" +
|
|
|
|
|
category + "' in the registry"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Tries to retrieve the value of the specified option</summary>
|
|
|
|
|
/// <typeparam name="TValue">Type the option will be converted to</typeparam>
|
|
|
|
|
/// <param name="category">Category the option can be found in. Can be null.</param>
|
|
|
|
|
/// <param name="optionName">Name of the option that will be looked up</param>
|
|
|
|
|
/// <param name="value">Will receive the value of the option, if found</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// True if the option existed and its value was written into the <paramref name="value" />
|
|
|
|
|
/// parameter, false otherwise
|
|
|
|
|
/// </returns>
|
2014-07-19 22:16:41 +00:00
|
|
|
|
public bool TryGet<TValue>(string category, string optionName, out TValue value) {
|
2014-07-22 10:43:23 +00:00
|
|
|
|
if(string.IsNullOrEmpty(category)) {
|
2014-07-22 11:10:14 +00:00
|
|
|
|
return tryGetValueFromKey(this.rootKey, optionName, out value);
|
2014-07-22 10:43:23 +00:00
|
|
|
|
} else {
|
|
|
|
|
using(RegistryKey categoryKey = this.rootKey.OpenSubKey(category, this.writable)) {
|
2014-07-22 11:10:14 +00:00
|
|
|
|
return tryGetValueFromKey(this.rootKey, optionName, out value);
|
2014-07-22 10:43:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-19 22:16:41 +00:00
|
|
|
|
}
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Saves an option in the settings store</summary>
|
|
|
|
|
/// <typeparam name="TValue">Type of value that will be saved</typeparam>
|
|
|
|
|
/// <param name="category">Category the option will be placed in. Can be null.</param>
|
|
|
|
|
/// <param name="optionName">Name of the option that will be saved</param>
|
|
|
|
|
/// <param name="value">The value under which the option will be saved</param>
|
2014-07-19 22:16:41 +00:00
|
|
|
|
public void Set<TValue>(string category, string optionName, TValue value) {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Removes the option with the specified name</summary>
|
|
|
|
|
/// <param name="category">Category the option is found in. Can be null.</param>
|
|
|
|
|
/// <param name="optionName">Name of the option that will be removed</param>
|
|
|
|
|
/// <returns>True if the option was found and removed</returns>
|
2014-07-19 22:16:41 +00:00
|
|
|
|
public bool Remove(string category, string optionName) {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2014-07-19 11:04:19 +00:00
|
|
|
|
|
2014-07-22 11:10:14 +00:00
|
|
|
|
/// <summary>Tries to retrieve the value of a registry key if it exists</summary>
|
|
|
|
|
/// <typeparam name="TValue">Type of value the registry key is expected to have</typeparam>
|
|
|
|
|
/// <param name="categoryKey">Registry key the value is stored under</param>
|
|
|
|
|
/// <param name="optionName">Name of the option in the registry</param>
|
|
|
|
|
/// <param name="value">Will receive the value read from the registry</param>
|
|
|
|
|
/// <returns>True if the value was found, false otherwise</returns>
|
|
|
|
|
private bool tryGetValueFromKey<TValue>(
|
|
|
|
|
RegistryKey categoryKey, string optionName, out TValue value
|
|
|
|
|
) {
|
|
|
|
|
object valueAsObject = categoryKey.GetValue(optionName);
|
|
|
|
|
if(valueAsObject == null) {
|
|
|
|
|
value = default(TValue);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(typeof(TValue) == typeof(bool)) {
|
|
|
|
|
string valueAsString = (string)Convert.ChangeType(
|
|
|
|
|
valueAsObject, typeof(string), CultureInfo.InvariantCulture
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
bool? boolean = ParserHelper.ParseBooleanLiteral(valueAsString);
|
|
|
|
|
if(boolean.HasValue) {
|
|
|
|
|
value = (TValue)(object)boolean.Value;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
"The value '" + valueAsString + "' can not be intepreted as a boolean"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
value = (TValue)Convert.ChangeType(
|
|
|
|
|
valueAsObject, typeof(TValue), CultureInfo.InvariantCulture
|
|
|
|
|
);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 10:43:23 +00:00
|
|
|
|
/// <summary>Figures out which .NET type best matches the registry value</summary>
|
|
|
|
|
/// <param name="categoryKey">Registry key the key is stored in</param>
|
|
|
|
|
/// <param name="optionName">Name of the option that will be retrieved</param>
|
|
|
|
|
/// <returns>The best matching .NET type for the registry key's value</returns>
|
|
|
|
|
private static Type getBestMatchingType(RegistryKey categoryKey, string optionName) {
|
|
|
|
|
RegistryValueKind valueKind = categoryKey.GetValueKind(optionName);
|
|
|
|
|
switch(valueKind) {
|
|
|
|
|
case RegistryValueKind.Binary: { return typeof(byte[]); }
|
|
|
|
|
case RegistryValueKind.DWord: { return typeof(int); }
|
|
|
|
|
case RegistryValueKind.QWord: { return typeof(long); }
|
|
|
|
|
case RegistryValueKind.MultiString: { return typeof(string[]); }
|
|
|
|
|
case RegistryValueKind.ExpandString:
|
|
|
|
|
case RegistryValueKind.String: {
|
|
|
|
|
string value = (string)categoryKey.GetValue(optionName);
|
|
|
|
|
if(value.Length == 0) {
|
|
|
|
|
return typeof(string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there are at least two characters, it may be an integer with
|
|
|
|
|
// a sign in front of it
|
|
|
|
|
if(value.Length >= 2) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
if(ParserHelper.SkipInteger(value, ref index)) {
|
|
|
|
|
if(index >= value.Length) {
|
|
|
|
|
return typeof(int);
|
|
|
|
|
}
|
|
|
|
|
if(value[index] == '.') {
|
|
|
|
|
return typeof(float);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { // If it's just a single character, it may be a number
|
|
|
|
|
if(char.IsNumber(value, 0)) {
|
|
|
|
|
return typeof(int);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it parses as a boolean literal, then it must be a boolean
|
|
|
|
|
if(ParserHelper.ParseBooleanLiteral(value) != null) {
|
|
|
|
|
return typeof(bool);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return typeof(string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case RegistryValueKind.Unknown:
|
|
|
|
|
case RegistryValueKind.None:
|
|
|
|
|
default: { return typeof(string); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Key on which the registry store is operating</summary>
|
|
|
|
|
private RegistryKey rootKey;
|
|
|
|
|
/// <summary>Whether the user can write to the registry key</summary>
|
|
|
|
|
private bool writable;
|
|
|
|
|
|
2014-07-19 11:04:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-19 22:16:41 +00:00
|
|
|
|
} // namespace Nuclex.Support.Settings
|
2014-07-22 08:50:05 +00:00
|
|
|
|
|
|
|
|
|
#endif // WINDOWS
|