Removing all unnecessary old files

This commit is contained in:
xRushG
2024-06-11 09:07:04 +02:00
parent 08eb35a360
commit 8b3a68f58e
11 changed files with 0 additions and 1743 deletions

View File

@@ -1,55 +0,0 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
namespace mRemoteNG.Tools.WindowsRegistry
{
[SupportedOSPlatform("windows")]
/// <summary>
/// Interface for the RegistryWorker class providing functionality to interact with the Windows Registry to retrieve registry settings.
/// </summary>
public interface IRegistryAdvanced
{
#region WindowsRegistry reader
string[] GetSubKeyNames(RegistryHive hive, string path);
string GetPropertyValue(WindowsRegistryKey key);
string GetPropertyValue(RegistryHive hive, string path, string name);
bool GetBoolValue(RegistryHive hive, string path, string propertyName, bool defaultValue = false);
int GetDwordValue(RegistryHive hive, string path, string propertyName, int defaultValue = 0);
WindowsRegistryKey GetWindowsRegistryKey(RegistryHive hive, string path, string name);
WindowsRegistryKey GetWindowsRegistryKey(WindowsRegistryKey key);
List<WindowsRegistryKey> GetRegistryEntries(RegistryHive hive, string path);
List<WindowsRegistryKey> GetRegistryEntryiesRecursive(RegistryHive hive, string path);
#endregion
#region WindowsRegistry writer
void SetRegistryValue(WindowsRegistryKey key);
void SetRegistryValue(RegistryHive hive, string path, string name, object value, RegistryValueKind valueKind);
void DeleteRegistryKey(RegistryHive hive, string path, bool ignoreNotFound = false);
#endregion
#region WindowsRegistry tools
RegistryHive ConvertStringToRegistryHive(string hiveString);
RegistryValueKind ConvertStringToRegistryValueKind(string valueType);
RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType);
Type ConvertRegistryValueKindToType(RegistryValueKind valueKind);
#endregion
#region WindowsRegistryAdvanced GetInteger
WindowsRegistryKeyInteger GetInteger(RegistryHive hive, string path, string propertyName, int? defaultValue = null);
#endregion
#region WindowsRegistryAdvanced GetString
WindowsRegistryKeyString GetString(RegistryHive hive, string path, string propertyName, string defaultValue = null);
WindowsRegistryKeyString GetStringValidated(RegistryHive hive, string path, string propertyName, string[] allowedValues, bool caseSensitive = false, string defaultValue = null);
#endregion
#region WindowsRegistryAdvanced GetBoolean
WindowsRegistryKeyBoolean GetBoolean(RegistryHive hive, string path, string propertyName, bool? defaultValue = null);
#endregion
}
}

View File

@@ -1,49 +0,0 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
namespace mRemoteNG.Tools.WindowsRegistry
{
[SupportedOSPlatform("windows")]
/// <summary>
/// Interface for the RegistryWorker class providing functionality to interact with the Windows Registry to retrieve registry settings.
/// </summary>
public interface IRegistryAdvancedRead
{
#region WindowsRegistry reader
string[] GetSubKeyNames(RegistryHive hive, string path);
string GetPropertyValue(WindowsRegistryKey key);
string GetPropertyValue(RegistryHive hive, string path, string name);
bool GetBoolValue(RegistryHive hive, string path, string propertyName, bool defaultValue = false);
int GetDwordValue(RegistryHive hive, string path, string propertyName, int defaultValue = 0);
WindowsRegistryKey GetWindowsRegistryKey(RegistryHive hive, string path, string name);
WindowsRegistryKey GetWindowsRegistryKey(WindowsRegistryKey key);
List<WindowsRegistryKey> GetRegistryEntries(RegistryHive hive, string path);
List<WindowsRegistryKey> GetRegistryEntryiesRecursive(RegistryHive hive, string path);
#endregion
#region WindowsRegistry tools
RegistryHive ConvertStringToRegistryHive(string hiveString);
RegistryValueKind ConvertStringToRegistryValueKind(string valueType);
RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType);
Type ConvertRegistryValueKindToType(RegistryValueKind valueKind);
#endregion
#region WindowsRegistryAdvanced GetInteger
WindowsRegistryKeyInteger GetInteger(RegistryHive hive, string path, string propertyName, int? defaultValue = null);
#endregion
#region WindowsRegistryAdvanced GetString
WindowsRegistryKeyString GetString(RegistryHive hive, string path, string propertyName, string defaultValue = null);
WindowsRegistryKeyString GetStringValidated(RegistryHive hive, string path, string propertyName, string[] allowedValues, bool caseSensitive = false, string defaultValue = null);
#endregion
#region WindowsRegistryAdvanced GetBoolean
WindowsRegistryKeyBoolean GetBoolean(RegistryHive hive, string path, string propertyName, bool? defaultValue = null);
#endregion
}
}

View File

@@ -1,26 +0,0 @@
using Microsoft.Win32;
using System;
using System.Runtime.Versioning;
namespace mRemoteNG.Tools.WindowsRegistry
{
[SupportedOSPlatform("windows")]
/// <summary>
/// Interface for the Registry class providing methods for interacting with write actions in the Windows Registry.
/// </summary>
public interface IRegistryWrite
{
#region registry writer
void SetRegistryValue(WindowsRegistryKey key);
void SetRegistryValue(RegistryHive hive, string path, string name, object value, RegistryValueKind valueKind);
#endregion
#region registry tools
RegistryHive ConvertStringToRegistryHive(string hiveString);
RegistryValueKind ConvertStringToRegistryValueKind(string valueType);
RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType);
Type ConvertRegistryValueKindToType(RegistryValueKind valueKind);
#endregion
}
}

View File

@@ -1,531 +0,0 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Security;
namespace mRemoteNG.Tools.WindowsRegistry
{
/// <summary>
/// This class interacting with the Windows Registry.
/// </summary>
[SupportedOSPlatform("windows")]
public class WindowsRegistry : IRegistry, IRegistryRead, IRegistryWrite
{
#region public read
/// <summary>
/// Retrieves the names of subkeys under a specified registry key path.
/// </summary>
/// <param name="hive">The RegistryHive where the subkeys are located.</param>
/// <param name="path">The path to the registry key containing the subkeys.</param>
/// <returns>An array of strings containing the names of subkeys, or an empty array if no subkeys are found.</returns>
public string[] GetSubKeyNames(RegistryHive hive, string path)
{
if (hive == 0)
throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive));
path.ThrowIfNull(nameof(path));
using (DisposableOptional<RegistryKey> key = OpenSubKey(hive, path))
{
return key.Any()
? key.First().GetSubKeyNames()
: new string[0];
}
}
/// <summary>
/// Retrieves the value of a specific property within a Windows Registry key and returns it as an Optional<string>.
/// </summary>
/// <param name="key">The WindowsRegistryKey containing information about the registry property.</param>
/// <returns>An Optional<string> containing the property value, or Optional<string>.Empty if the value is not found.</returns>
public string GetPropertyValue(WindowsRegistryKey key)
{
if (!key.IsKeyReadable())
throw new InvalidOperationException("The Windows Registry key is not ready for reading.");
return GetPropertyValue(key.Hive, key.Path, key.Name);
}
/// <summary>
/// Retrieves the value of a specific property within the Windows Registry and returns it as an Optional<string>.
/// </summary>
/// <param name="hive">The RegistryHive where the property is located.</param>
/// <param name="path">The path to the registry key containing the property.</param>
/// <param name="name">The name of the property to retrieve.</param>
/// <returns>An Optional<string> containing the property value, or Optional<string>.Empty if the value is not found.</returns>
public string GetPropertyValue(RegistryHive hive, string path, string name)
{
if (hive == 0)
throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive));
path.ThrowIfNull(nameof(path));
name.ThrowIfNull(nameof(name));
using (DisposableOptional<RegistryKey> key = OpenSubKey(hive, path))
{
if (!key.Any())
return null;
object keyValue = key.First().GetValue(name);
if (keyValue == null)
return null;
return keyValue.ToString();
}
}
/// <summary>
/// Gets a boolean value from the Windows Registry based on the specified registry path and property name.
/// If the value is not found or cannot be parsed, it returns a specified default value.
/// </summary>
/// <param name="hive">The Registry hive where the value is located.</param>
/// <param name="path">The registry path to the key containing the property.</param>
/// <param name="propertyName">The name of the property to retrieve.</param>
/// <param name="defaultValue">The default value to return if the property is not found or cannot be parsed. Default is false.</param>
/// <returns>The boolean value of the specified property or the default value if not found or cannot be parsed.</returns>
public bool GetBoolValue(RegistryHive hive, string path, string propertyName, bool defaultValue = false)
{
string value = GetPropertyValue(hive, path, propertyName);
if (!string.IsNullOrEmpty(value))
{
if (int.TryParse(value, out int intValue))
return intValue == 1;
if (bool.TryParse(value, out bool boolValue))
return boolValue;
}
return defaultValue;
}
// <summary>
/// Retrieves a DWORD value from the Windows Registry, with an optional default value.
/// </summary>
/// <param name="hive">The Registry hive to access.</param>
/// <param name="path">The Registry path containing the key.</param>
/// <param name="propertyName">The name of the Registry property.</param>
/// <param name="defaultValue">The default value to return if the property is not found or cannot be parsed.</param>
/// <returns>The DWORD value from the Registry, or the specified default value.</returns>
public int GetDwordValue(RegistryHive hive, string path, string propertyName, int defaultValue = 0)
{
string value = GetPropertyValue(hive, path, propertyName);
if (int.TryParse(value, out int intValue))
{
return intValue;
}
return defaultValue;
}
/// <summary>
/// Retrieves a WindowsRegistryKey object for a specific registry hive, path, and value name.
/// </summary>
/// <param name="hive">The RegistryHive of the key.</param>
/// <param name="path">The path of the key.</param>
/// <param name="name">The name of the value to retrieve.</param>
/// <returns>A WindowsRegistryKey object representing the specified registry key and value.</returns>
public WindowsRegistryKey GetWindowsRegistryKey(RegistryHive hive, string path, string name)
{
WindowsRegistryKey key = new()
{
Hive = hive,
Path = path,
Name = name
};
return GetWindowsRegistryKey(key);
}
/// <summary>
/// Retrieves a WindowsRegistryKey object for a specific WindowsRegistryKey, populating its value and value kind.
/// </summary>
/// <param name="key">A WindowsRegistryKey object containing the hive, path, value name and more.</param>
/// <returns>A WindowsRegistryKey object representing the specified registry key and value, with value and value kind populated.</returns>
public WindowsRegistryKey GetWindowsRegistryKey(WindowsRegistryKey key)
{
if (!key.IsKeyReadable())
throw new InvalidOperationException("The Windows Registry key is not ready for reading.");
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(key.Hive, RegistryView.Default), subKey = baseKey.OpenSubKey(key.Path))
{
if (subKey != null)
{
object value = subKey.GetValue(key.Name);
if (value != null)
key.Value = value.ToString();
if (TestValueKindExists(subKey, key.Name, out RegistryValueKind ValueKind))
key.ValueKind = ValueKind;
}
}
return key;
}
/// <summary>
/// Retrieves a list of registry entries (properties) and their values under a given key path.
/// </summary>
/// <param name="hive">The RegistryHive of the key path.</param>
/// <param name="path">The path of the key from which to retrieve values.</param>
/// <returns>A list of WindowsRegistryKey objects, each representing a value within the specified registry key path.</returns>
public List<WindowsRegistryKey> GetRegistryEntries(RegistryHive hive, string path)
{
if (hive == 0)
throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive));
path.ThrowIfNull(nameof(path));
List<WindowsRegistryKey> list = [];
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default), key = baseKey.OpenSubKey(path))
{
if (key != null)
{
foreach (string valueName in key.GetValueNames())
{
list.Add(new WindowsRegistryKey
{
Hive = hive,
Path = path,
Name = valueName,
Value = key.GetValue(valueName).ToString(),
ValueKind = key.GetValueKind(valueName)
}
);
}
}
}
return list;
}
/// <summary>
/// Recursively retrieves registry entries under a given key path and its subkeys.
/// </summary>
/// <param name="hive">The RegistryHive of the key path.</param>
/// <param name="path">The path of the key from which to retrieve values.</param>
/// <returns>A list of WindowsRegistryKey objects, each representing a value within the specified registry key path.</returns>
public List<WindowsRegistryKey> GetRegistryEntryiesRecursive(RegistryHive hive, string path)
{
if (hive == 0)
throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive));
path.ThrowIfNull(nameof(path));
List<WindowsRegistryKey> list = GetRegistryEntries(hive, path);
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default), key = baseKey.OpenSubKey(path))
{
if (key != null)
{
foreach (string subPathName in key.GetSubKeyNames())
{
string subKey = $"{path}\\{subPathName}";
list.AddRange(GetRegistryEntryiesRecursive(hive, subKey));
}
}
}
return list;
}
#endregion
#region public write methods
/// <summary>
/// Sets the value of a specific property within a registry key using individual parameters.
/// </summary>
/// <param name="hive">The registry hive.</param>
/// <param name="path">The path to the registry key containing the property.</param>
/// <param name="name">The name of the property to set.</param>
/// <param name="value">The value to set for the property.</param>
/// <param name="valueKind">The data type of the value to set.</param>
public void SetRegistryValue(RegistryHive hive, string path, string name, object value, RegistryValueKind valueKind)
{
WindowsRegistryKey key = (new WindowsRegistryKey
{
Hive = hive,
Path = path,
Name = name,
Value = value.ToString(),
ValueKind = valueKind
});
CreateOrSetRegistryValue(key);
}
/// <summary>
/// Sets the value of a specific property within a registry key using the provided WindowsRegistryKey object.
/// </summary>
/// <param name="key">The WindowsRegistryKey object containing information about the registry property.</param>
public void SetRegistryValue(WindowsRegistryKey key)
{
CreateOrSetRegistryValue(key);
}
/// <summary>
/// Deletes a registry key and its subkeys.
/// </summary>
/// <param name="hive">The registry hive to open.</param>
/// <param name="path">The path of the registry key to delete.</param>
/// <param name="ignoreNotFound">Set to true to ignore if the key is not found.</param>
public void DeleteRegistryKey(RegistryHive hive, string path, bool ignoreNotFound = false)
{
try
{
using (RegistryKey key = RegistryKey.OpenBaseKey(hive, RegistryView.Default))
{
if (key != null)
{
key.DeleteSubKeyTree(path, ignoreNotFound);
}
}
}
catch (Exception ex)
{
// Handle any exceptions according to your requirements
Console.WriteLine($"Error deleting registry key: {ex.Message}");
throw;
}
}
#endregion
#region public methods
/// <summary>
/// Converts a string representation of a Registry Hive to the corresponding RegistryHive enum value.
/// </summary>
/// <param name="hiveString">A string representation of a Registry Hive, not case-sensitive.</param>
/// <returns>The RegistryHive enum value corresponding to the provided string representation.</returns>
/// <exception cref="ArgumentException">Thrown if the provided string does not match a valid Registry Hive.</exception>
public RegistryHive ConvertStringToRegistryHive(string hiveString)
{
switch (hiveString.ToLower())
{
// ClassesRoot
case "hkcr":
case "hkey_classes_root":
case "classesroot":
return RegistryHive.ClassesRoot;
// CurrentUser
case "hkcu":
case "hkey_current_user":
case "currentuser":
return RegistryHive.CurrentUser;
// LocalMachine
case "hklm":
case "hkey_local_machine":
case "localmachine":
return RegistryHive.LocalMachine;
// Users
case "hku":
case "hkey_users":
case "users":
return RegistryHive.Users;
// CurrentConfig
case "hkcc":
case "hkey_current_config":
case "currentconfig":
return RegistryHive.CurrentConfig;
default:
throw new ArgumentException("Invalid registry hive string.", nameof(hiveString));
}
}
/// <summary>
/// Converts a string representation of a RegistryValueKind to the corresponding RegistryValueKind enum value.
/// </summary>
/// <param name="valueType">A string representation of a RegistryValueKind, not case-sensitive.</param>
/// <returns>The RegistryValueKind enum value corresponding to the provided string representation.</returns>
/// <exception cref="ArgumentException">Thrown if the provided string does not match a valid RegistryValueKind.</exception>
public RegistryValueKind ConvertStringToRegistryValueKind(string valueType)
{
switch (valueType.ToLower())
{
// REG_SZ
case "string":
case "reg_sz":
return RegistryValueKind.String;
// REG_DWORD
case "dword":
case "reg_dword":
return RegistryValueKind.DWord;
// REG_BINARY
case "binary":
case "reg_binary":
return RegistryValueKind.Binary;
// REG_QWORD
case "qword":
case "reg_qword":
return RegistryValueKind.QWord;
// REG_MULTI_SZ
case "multistring":
case "reg_multi_sz":
return RegistryValueKind.MultiString;
// REG_EXPAND_SZ
case "expandstring":
case "reg_expand_sz":
return RegistryValueKind.ExpandString;
default:
throw new ArgumentException("Invalid RegistryValueKind string representation.", nameof(valueType));
}
}
/// <summary>
/// Converts a .NET data type to the corresponding RegistryValueKind.
/// </summary>
/// <param name="valueType">The .NET data type to convert.</param>
/// <returns>The corresponding RegistryValueKind.</returns>
public RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType)
{
switch (Type.GetTypeCode(valueType))
{
case TypeCode.String:
return RegistryValueKind.String;
case TypeCode.Int32:
return RegistryValueKind.DWord;
case TypeCode.Int64:
return RegistryValueKind.QWord;
case TypeCode.Boolean:
return RegistryValueKind.DWord;
case TypeCode.Byte:
return RegistryValueKind.Binary;
/*
case TypeCode.Single:
return RegistryValueKind;
case TypeCode.Double:
return RegistryValueKind.String;
case TypeCode.DateTime:
return RegistryValueKind.String; // DateTime can be stored as a string or other types
case TypeCode.Char:
return RegistryValueKind.String; // Char can be stored as a string or other types
case TypeCode.Decimal:
return RegistryValueKind.String; // Decimal can be stored as a string or other types
*/
default:
return RegistryValueKind.String; // Default to String for unsupported types
}
}
/// <summary>
/// Converts a RegistryValueKind enumeration value to its corresponding .NET Type.
/// </summary>
/// <param name="valueKind">The RegistryValueKind value to be converted.</param>
/// <returns>The .NET Type that corresponds to the given RegistryValueKind.</returns>
public Type ConvertRegistryValueKindToType(RegistryValueKind valueKind)
{
switch (valueKind)
{
case RegistryValueKind.String:
case RegistryValueKind.ExpandString:
return typeof(string);
case RegistryValueKind.DWord:
return typeof(int);
case RegistryValueKind.QWord:
return typeof(long);
case RegistryValueKind.Binary:
return typeof(byte[]);
case RegistryValueKind.MultiString:
return typeof(string[]);
case RegistryValueKind.Unknown:
default:
return typeof(object);
}
}
#endregion
#region private methods
/// <summary>
/// Opens a subkey within the Windows Registry under the specified hive and key path.
/// </summary>
/// <param name="hive">The Windows Registry hive where the subkey is located.</param>
/// <param name="keyPath">The path to the subkey to be opened.</param>
/// <returns>
/// A disposable optional object containing the opened registry subkey if successful;
/// otherwise, it returns an empty optional object.
/// </returns>
private DisposableOptional<RegistryKey> OpenSubKey(RegistryHive hive, string keyPath)
{
switch (hive)
{
case RegistryHive.ClassesRoot:
return new DisposableOptional<RegistryKey>(Registry.ClassesRoot.OpenSubKey(keyPath));
case RegistryHive.CurrentConfig:
return new DisposableOptional<RegistryKey>(Registry.CurrentConfig.OpenSubKey(keyPath));
case RegistryHive.CurrentUser:
return new DisposableOptional<RegistryKey>(Registry.CurrentUser.OpenSubKey(keyPath));
case RegistryHive.Users:
return new DisposableOptional<RegistryKey>(Registry.Users.OpenSubKey(keyPath));
case RegistryHive.LocalMachine:
return new DisposableOptional<RegistryKey>(Registry.LocalMachine.OpenSubKey(keyPath));
default:
throw new ArgumentOutOfRangeException(nameof(hive), hive, null);
}
}
/// <summary>
/// Attempts to retrieve the value kind of a specific property within a registry subkey.
/// </summary>
/// <param name="subKey">The registry subkey from which to retrieve the value kind.</param>
/// <param name="valueName">The name of the property for which to retrieve the value kind.</param>
/// <param name="valueKind">An output parameter that will contain the retrieved value kind, or RegistryValueKind.Unknown if the property or value kind is not found.</param>
/// <returns>True if the operation is successful, otherwise false.</returns>
private bool TestValueKindExists(RegistryKey subKey, string valueName, out RegistryValueKind valueKind)
{
// Set a default value
valueKind = RegistryValueKind.Unknown;
try
{
valueKind = subKey.GetValueKind(valueName);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Creates or sets the value of a specific property within a registry key.
/// </summary>
/// <param name="key">The WindowsRegistryKey object containing information about the registry property.</param>
/// <exception cref="InvalidOperationException">Thrown when error by writing to the Windows Registry key.</exception>
private void CreateOrSetRegistryValue(WindowsRegistryKey key)
{
try
{
if (!key.IsKeyWritable())
throw new ArgumentNullException("The Windows Registry key is not ready for writing.");
using RegistryKey baseKey = RegistryKey.OpenBaseKey(key.Hive, RegistryView.Default);
RegistryKey registryKey = baseKey.OpenSubKey(key.Path, true);
if (registryKey == null)
{
// The specified subkey doesn't exist, so create it.
using RegistryKey newKey = baseKey.CreateSubKey(key.Path);
newKey.SetValue(key.Name, key.Value, key.ValueKind);
}
else
{
registryKey.SetValue(key.Name, key.Value, key.ValueKind);
}
}
catch (Exception ex)
{
throw new InvalidOperationException("Error writing to the Windows Registry key.", ex);
}
}
#endregion
}
}

View File

@@ -1,118 +0,0 @@
using Microsoft.Win32;
using mRemoteNG.App.Info;
using mRemoteNG.Security.SymmetricEncryption;
using System.Runtime.Versioning;
namespace mRemoteNG.Tools.WindowsRegistry
{
[SupportedOSPlatform("windows")]
/// <summary>
/// Extends the functionality of interacting with the Windows Registry, building upon the base WindowsRegistry class.
/// </summary>
public class WindowsRegistryAdvanced : WindowsRegistry , IRegistryAdvanced, IRegistryAdvancedRead
{
#region dword methods
/// <summary>
/// Retrieves a DWORD (32-bit integer) value from the Windows Registry based on the specified registry information.
/// </summary>
/// <param name="hive">The registry hive.</param>
/// <param name="path">The path to the registry key.</param>
/// <param name="propertyName">The name of the registry property.</param>
/// <param name="defaultValue">Optional default value to be used if the registry key is not present (default is null).</param>
/// <returns>A WindowsRegistryKeyInteger instance representing the retrieved DWORD value.</returns>
public WindowsRegistryKeyInteger GetInteger(RegistryHive hive, string path, string propertyName, int? defaultValue = null)
{
// Retrieve the Windows Registry key
WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName);
// Create a WindowsRegistryKeyInteger instance and initialize it from the retrieved key
WindowsRegistryKeyInteger IntKey = new();
IntKey.ConvertFromWindowsRegistryKey(key);
return IntKey;
}
#endregion
#region string methods
/// <summary>
/// Retrieves a string value from the Windows Registry based on the specified registry information.
/// </summary>
/// <param name="hive">The registry hive.</param>
/// <param name="path">The path to the registry key.</param>
/// <param name="propertyName">The name of the registry property.</param>
/// <param name="defaultValue">Optional default value to be used if the registry key is not present (default is null).</param>
/// <returns>A WindowsRegistryKeyString instance representing the retrieved string value.</returns>
public WindowsRegistryKeyString GetString(RegistryHive hive, string path, string propertyName, string defaultValue = null)
{
// Retrieve the Windows Registry key
WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName);
// Create a WindowsRegistryKeyString instance and initialize it from the retrieved key
WindowsRegistryKeyString StrKey = new();
StrKey.ConvertFromWindowsRegistryKey(key, defaultValue);
return StrKey;
}
/// <summary>
/// Retrieves a string value from the Windows Registry based on the specified registry information and validates it against a set of allowed values.
/// </summary>
/// <param name="hive">The registry hive.</param>
/// <param name="path">The path to the registry key.</param>
/// <param name="propertyName">The name of the registry property.</param>
/// <param name="allowedValues">An array of valid values against which the retrieved string is validated.</param>
/// <param name="caseSensitive">Optional parameter indicating whether the validation is case-sensitive (default is false).</param>
/// <returns>A WindowsRegistryKeyString instance representing the retrieved and validated string value.</returns>
public WindowsRegistryKeyString GetStringValidated(RegistryHive hive, string path, string propertyName, string[] allowedValues, bool caseSensitive = false, string defaultValue = null)
{
// Retrieve the Windows Registry key
WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName);
// Create a WindowsRegistryKeyString instance and initialize it from the retrieved key
WindowsRegistryKeyString StrKey = new()
{
AllowedValues = allowedValues,
IsCaseSensitiveValidation = caseSensitive
};
StrKey.ConvertFromWindowsRegistryKey(key, defaultValue);
return StrKey;
}
/*public WindowsRegistryKeySecureString GetSecureString(RegistryHive hive, string path, string propertyName)
{
// Retrieve the Windows Registry key, the key should be encrypted
var key = GetWindowsRegistryKey(hive, path, propertyName);
// Create a WindowsRegistryKeyBoolean instance and initialize it from the retrieved key
WindowsRegistryKeySecureString secureKey = new (); // class not exsists
secureKey.ConvertFromWindowsRegistryKey(key); // no default possible!
return secureKey
}*/
#endregion
#region bool methods
/// <summary>
/// Retrieves a boolean value from the Windows Registry based on the specified registry information.
/// </summary>
/// <param name="hive">The registry hive.</param>
/// <param name="path">The path to the registry key.</param>
/// <param name="propertyName">The name of the registry property.</param>
/// <param name="defaultValue">An optional default value to use if the registry key is not present or if the value is not a valid boolean.</param>
/// <returns>A WindowsRegistryKeyBoolean instance representing the retrieved boolean value.</returns>
public WindowsRegistryKeyBoolean GetBoolean(RegistryHive hive, string path, string propertyName, bool? defaultValue = null)
{
// Retrieve the Windows Registry key
WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName);
// Create a WindowsRegistryKeyBoolean instance and initialize it from the retrieved key
WindowsRegistryKeyBoolean boolKey = new ();
boolKey.ConvertFromWindowsRegistryKey(key, defaultValue);
return boolKey;
}
#endregion
}
}

View File

@@ -1,116 +0,0 @@
using System;
using System.Runtime.Versioning;
using Microsoft.Win32;
namespace mRemoteNG.Tools.WindowsRegistry
{
/// <summary>
/// Represents a Windows Registry key with a default string value, providing a flexible abstraction for registry operations.
/// This class can be extended by inherited classes to customize behavior for specific data types.
/// </summary>
[SupportedOSPlatform("windows")]
public class WindowsRegistryKey
{
#region properties
#region Property registryHive
public RegistryHive Hive
{
get { return _Hive; }
set
{
if (value == 0)
throw new ArgumentNullException("RegistryHive unknown.");
_Hive = value;
}
}
private RegistryHive _Hive { get; set; }
#endregion
#region Property path
public string Path
{
get { return _Path; }
set
{
value.ThrowIfNull(nameof(value));
_Path = value;
}
}
private string _Path { get; set; }
#endregion
#region Property name
public string Name
{
get { return _Name; }
set
{
value.ThrowIfNull(nameof(value));
_Name = value;
}
}
private string _Name { get; set; }
#endregion
#region Property value
public virtual string Value
{
get => _value;
set
{
_value = value;
UpdateIsProvidedState();
}
}
private string _value;
#endregion
public RegistryValueKind ValueKind { get; set; } = RegistryValueKind.Unknown;
public bool IsKeyPresent { get; set; } = false;
#endregion
#region public methods
/// <summary>
/// Checks if the Windows Registry key is ready for reading by ensuring that the hive,
/// path, and name properties are set.
/// </summary>
/// <returns>True if the key is ready for reading, otherwise false.</returns>
public bool IsKeyReadable() {
return (IsHiveSet() && IsPathSet() && IsNameSet());
}
/// <summary>
/// Checks if the Windows Registry key is ready for a write operation.
/// The key is considered write-ready if none of the following conditions are met:
/// - The hive is set
/// - The registry value type is set
/// - The key path is set
/// - The value name is set
/// </summary>
/// <returns>Returns true if the key is write-ready, otherwise false.</returns>
public bool IsKeyWritable() {
return (IsHiveSet() && IsValueKindSet() && IsPathSet() && IsNameSet());
}
#endregion
#region protected methods
protected void UpdateIsProvidedState()
{
// Key is present when RegistryKey value is not null
IsKeyPresent = Value != null;
}
protected bool IsHiveSet() => Hive != 0;
protected bool IsValueKindSet() => ValueKind != 0;
protected bool IsPathSet() => Path != null;
protected bool IsNameSet() => Name != null;
#endregion
}
}

View File

@@ -1,75 +0,0 @@
using System;
using System.Runtime.Versioning;
namespace mRemoteNG.Tools.WindowsRegistry
{
/// <summary>
/// Represents a boolean Windows Registry key, extending the base class WindowsRegistryKey.
/// </summary>
[SupportedOSPlatform("windows")]
public class WindowsRegistryKeyBoolean : WindowsRegistryKey
{
/// <summary>
/// Gets or sets the default boolean value for a Windows Registry key.
/// </summary>
/// <remarks>
/// The default value is initially set to `false`.
/// </remarks>
public bool DefaultValue { get; private set; } = false;
/// <summary>
/// Gets or sets the boolean value for a Windows Registry key.
/// </summary>
public new bool Value
{
get => BoolValue;
private set
{
BoolValue = value;
UpdateIsProvidedState();
}
}
private bool BoolValue;
/// <summary>
/// Converts and initializes a WindowsRegistryKeyBoolean from a base WindowsRegistryKey, with an optional default value.
/// </summary>
/// <param name="baseKey">The base WindowsRegistryKey to convert from.</param>
/// <param name="defaultValue">Optional default value to set for the WindowsRegistryKeyBoolean.</param>
public void ConvertFromWindowsRegistryKey(WindowsRegistryKey baseKey, bool? defaultValue = null)
{
SetDefaultValue(defaultValue);
FromBaseKey(baseKey);
}
private void FromBaseKey(WindowsRegistryKey baseKey)
{
if (baseKey == null)
throw new ArgumentNullException(nameof(baseKey));
Hive = baseKey.Hive;
Path = baseKey.Path;
Name = baseKey.Name;
ValueKind = baseKey.ValueKind;
IsKeyPresent = baseKey.IsKeyPresent;
ConvertToBool(baseKey.Value);
}
private void SetDefaultValue(bool? defaultValue)
{
if (defaultValue.HasValue)
DefaultValue = (bool)defaultValue;
}
private void ConvertToBool(string newValue)
{
if (IsKeyPresent && bool.TryParse(newValue, out bool boolValue))
BoolValue = boolValue;
else if (IsKeyPresent && int.TryParse(newValue, out int intValue))
BoolValue = intValue == 1;
else
BoolValue = DefaultValue;
}
}
}

View File

@@ -1,78 +0,0 @@
using Microsoft.Win32;
using System;
using System.Runtime.Versioning;
namespace mRemoteNG.Tools.WindowsRegistry
{
/// <summary>
/// Represents a integer Windows Registry key, extending the base class WindowsRegistryKey.
/// </summary>
[SupportedOSPlatform("windows")]
public class WindowsRegistryKeyInteger : WindowsRegistryKey
{
/// <summary>
/// Gets or sets the default integer value for a Windows Registry key.
/// </summary>
/// <remarks>
/// The default value is initially set to `-1`.
/// </remarks>
public int DefaultValue { get; private set; } = -1;
/// <summary>
/// Gets or sets the integer value for a Windows Registry key.
/// </summary>
public new int Value
{
get => IntegerValue;
private set
{
IntegerValue = value;
UpdateIsProvidedState();
}
}
private int IntegerValue;
/// <summary>
/// Converts and initializes a WindowsRegistryKeyInteger from a base WindowsRegistryKey, with an optional default value.
/// </summary>
/// <param name="baseKey">The base WindowsRegistryKey to convert from.</param>
/// <param name="defaultValue">Optional default value to set for the WindowsRegistryKeyBoolean.</param>
public void ConvertFromWindowsRegistryKey(WindowsRegistryKey baseKey, int? defaultValue = null)
{
SetDefaultValue(defaultValue);
FromBaseKey(baseKey);
}
private void FromBaseKey(WindowsRegistryKey baseKey)
{
if (baseKey == null)
throw new ArgumentNullException(nameof(baseKey));
Hive = baseKey.Hive;
Path = baseKey.Path;
Name = baseKey.Name;
ValueKind = baseKey.ValueKind;
IsKeyPresent = baseKey.IsKeyPresent;
ConvertToInteger(baseKey.Value);
}
private void SetDefaultValue (int? defaultValue)
{
if (defaultValue.HasValue)
DefaultValue = (int)defaultValue;
}
private void ConvertToInteger(string newValue)
{
if (ValueKind != RegistryValueKind.DWord)
IsKeyPresent = false;
if (IsKeyPresent && int.TryParse(newValue.ToString(), out int intValue))
IntegerValue = intValue;
else
IntegerValue = DefaultValue;
}
}
}

View File

@@ -1,121 +0,0 @@
using Microsoft.Win32;
using System;
using System.Linq;
using System.Runtime.Versioning;
namespace mRemoteNG.Tools.WindowsRegistry
{
/// <summary>
/// Represents a string Windows Registry key, extending the base class WindowsRegistryKey, can be evaluated with a value set.
/// </summary>
[SupportedOSPlatform("windows")]
public class WindowsRegistryKeyString : WindowsRegistryKey
{
/// <summary>
/// Gets or sets the default integer value for a Windows Registry key.
/// </summary>
public string DefaultValue { get; private set; }
/// <summary>
/// Gets or sets an array of allowed values for validation.
/// </summary>
public string[] AllowedValues { get; set; }
/// <summary>
/// Gets or sets a boolean flag indicating whether validation is case-sensitive.
/// </summary>
public bool IsCaseSensitiveValidation { get; set; } = false;
/// <summary>
/// Gets a boolean indicating whether the value is valid based on the validation rules.
/// </summary>
public bool IsValid { get; private set; } = false;
public new string Value
{
get => StringValue;
private set
{
StringValue = value;
UpdateIsProvidedState();
Validate();
}
}
private string StringValue;
/// <summary>
/// Converts and initializes a WindowsRegistryKeyString from a base WindowsRegistryKey, with an optional default value.
/// </summary>
/// <param name="baseKey">The base WindowsRegistryKey to convert from.</param>
/// <param name="defaultValue">Optional default value to set for the WindowsRegistryKeyBoolean.</param>
public void ConvertFromWindowsRegistryKey(WindowsRegistryKey baseKey, string defaultValue = null)
{
SetDefaultValue(defaultValue);
FromBaseKey(baseKey);
Validate();
}
private void FromBaseKey(WindowsRegistryKey baseKey)
{
if (baseKey == null)
throw new ArgumentNullException(nameof(baseKey));
Hive = baseKey.Hive;
Path = baseKey.Path;
Name = baseKey.Name;
ValueKind = baseKey.ValueKind;
IsKeyPresent = baseKey.IsKeyPresent;
ConvertToString(baseKey.Value);
}
private void SetDefaultValue (string defaultValue)
{
DefaultValue = defaultValue;
}
private void ConvertToString(string newValue)
{
if (IsKeyPresent && newValue != null)
StringValue = newValue;
else
StringValue = DefaultValue;
}
/// <summary>
/// Validates a Windows Registry key value against a set of allowed values, considering case sensitivity.
/// </summary>
/// <param name="allowedValues">Array of allowed values.</param>
/// <param name="caseSensitive">Optional parameter to specify case sensitivity in validation.</param>
public void Validate(string[] allowedValues = null, bool? caseSensitive = null)
{
// Key must be present to evaluate
if (!IsKeyPresent)
return;
if (caseSensitive.HasValue)
IsCaseSensitiveValidation = caseSensitive.Value;
if (allowedValues != null && allowedValues.Length >= 1)
AllowedValues = allowedValues;
// AllowedValues array cannot be null or empty.
if (AllowedValues == null || AllowedValues.Length == 0 || !IsKeyPresent)
return;
if (IsKeyPresent && AllowedValues.Any(v =>
IsCaseSensitiveValidation ? v == Value : v.Equals(Value, StringComparison.OrdinalIgnoreCase)))
{
// Set to true when the value is found in the valid values
IsValid = true;
}
else
{
// Set to false when the value is not found in the valid values
IsValid = false;
StringValue = DefaultValue;
}
}
}
}

View File

@@ -1,188 +0,0 @@
using Microsoft.Win32;
using mRemoteNG.Tools.WindowsRegistry;
using NUnit.Framework;
namespace mRemoteNGTests.Tools.Registry
{
internal class WindowsRegistryAdvancedTests : WindowsRegistryAdvanced
{
private const string _TestRootKey = @"Software\mRemoteNGTest";
private const RegistryHive _TestHive = RegistryHive.CurrentUser;
[SetUp]
public void Setup()
{
// GetBoolean && GetBoolValue (GetBoolValue -> Not Advanced but not tested jet)
SetRegistryValue(_TestHive, _TestRootKey, "TestBoolAsString", "true", RegistryValueKind.String);
SetRegistryValue(_TestHive, _TestRootKey, "TestBoolAsDWord", 0, RegistryValueKind.DWord);
// GetInteger Tests
SetRegistryValue(_TestHive, _TestRootKey, "TestInteger", "4711", RegistryValueKind.DWord);
// GetString Tests
SetRegistryValue(_TestHive, _TestRootKey, "TestString1", "Banane", RegistryValueKind.String);
SetRegistryValue(_TestHive, _TestRootKey, "TestString2", "Hund", RegistryValueKind.String);
}
[TearDown]
public void Cleanup()
{
// Delete the registry keys here
DeleteRegistryKey(_TestHive, _TestRootKey, true);
}
#region GetBoolean() Tests
// Non object returns
[Test]
public void GetBooleanFromString_ReturnsTrue()
{
var key = GetBoolean(_TestHive, _TestRootKey, "TestBoolAsString");
Assert.That(key.Value, Is.EqualTo(true));
}
[Test]
public void GetBooleanFromDword_ReturnsFalse()
{
var key = GetBoolean(_TestHive, _TestRootKey, "TestBoolAsDWord");
Assert.That(key.Value, Is.EqualTo(false));
}
[Test]
public void GetBooleanNotProvided_ReturnsDefaultTrue()
{
var key = GetBoolean(_TestHive, _TestRootKey, "TestBoolNotProvided", true);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(true), "Value should be the default (true)");
Assert.That(key.IsKeyPresent, Is.EqualTo(false), "IsProvided should be false");
});
}
#endregion
#region GetBoolValue()___No Object, just bool value returns
[Test]
public void GetBoolValueFromString_ReturnsTrue()
{
var key = GetBoolValue(_TestHive, _TestRootKey, "TestBoolAsString");
Assert.That(key, Is.EqualTo(true));
}
[Test]
public void GetBoolValueFromDword_ReturnsFalse()
{
var key = GetBoolValue(_TestHive, _TestRootKey, "TestBoolAsDWord", true);
Assert.That(key, Is.EqualTo(false));
}
[Test]
public void GetBoolValue_ReturnsDefaultTrue()
{
var key = GetBoolValue(_TestHive, _TestRootKey, "TestBoolNotProvided", true);
Assert.That(key, Is.EqualTo(true));
}
#endregion
#region GetInteger()
[Test]
public void GetInteger()
{
var key = GetInteger(_TestHive, _TestRootKey, "TestInteger");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(4711));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
});
}
[Test]
public void GetInteger_returnObjectDefault()
{
var key = GetInteger(_TestHive, _TestRootKey, "TestIntegerNotProvided");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(-1), "Value should be the default (-1)");
Assert.That(key.IsKeyPresent, Is.EqualTo(false));
});
}
[Test]
public void GetInteger_returnSpecifiedDefault()
{
var key = GetInteger(_TestHive, _TestRootKey, "TestIntegerNotProvided", 2096);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(-1), "Value should be the default (-1)");
Assert.That(key.IsKeyPresent, Is.EqualTo(false));
});
}
[Test]
public void GetDwordValue_returnIntegerValue()
{
int value = GetDwordValue(_TestHive, _TestRootKey, "TestInteger");
Assert.That(value, Is.EqualTo(4711));
}
#endregion
#region GetString()
[Test]
public void GetString()
{
var key = GetString(_TestHive, _TestRootKey, "TestString1");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
});
}
[Test]
public void GetString_ReturnsDefault()
{
var key = GetString(_TestHive, _TestRootKey, "TestStringNotProvided", "Banane");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(false));
});
}
[Test]
public void GetStringValidated_Valid()
{
string[] fruits = { "Banane", "Erdbeere", "Apfel" };
var key = GetStringValidated(_TestHive, _TestRootKey, "TestString1", fruits);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
Assert.That(key.IsValid, Is.EqualTo(true));
});
}
[Test]
public void GetStringValidated_NotValidNull()
{
string[] fruits = { "Banane", "Erdbeere", "Apfel" };
var key = GetStringValidated(_TestHive, _TestRootKey, "TestString2", fruits);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(null));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
Assert.That(key.IsValid, Is.EqualTo(false));
});
}
[Test]
public void GetStringValidated_NotValidDefault()
{
string[] fruits = { "Banane", "Erdbeere", "Apfel" };
var key = GetStringValidated(_TestHive, _TestRootKey, "TestString2", fruits, false, "Banane");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
Assert.That(key.IsValid, Is.EqualTo(false));
});
}
#endregion
}
}

View File

@@ -1,386 +0,0 @@
using Microsoft.Win32;
using mRemoteNG.Tools.WindowsRegistry;
using NUnit.Framework;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
namespace mRemoteNGTests.Tools.Registry
{
internal class WindowsRegistryKeyTests
{
private WindowsRegistryKey CompleteRegistryKey { get; set; }
private WindowsRegistryKey PartialRegistryKey { get; set; }
[SetUp]
public void Setup()
{
CompleteRegistryKey = new WindowsRegistryKey()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.String,
Name = "Test",
Path = @"SOFTWARE\TEST\TEST\Test",
Value = "CompleteRegistryKey"
};
PartialRegistryKey = new WindowsRegistryKey()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
};
}
#region WindowsRegistryKey() tests
[Test]
public void WindowsRegistryKeyReadable()
{
Assert.That(CompleteRegistryKey.IsKeyReadable, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyNotReadable()
{
Assert.That(PartialRegistryKey.IsKeyReadable, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyWriteable()
{
Assert.That(CompleteRegistryKey.IsKeyWritable, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyNotWriteable()
{
Assert.That(PartialRegistryKey.IsKeyWritable, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyProvided()
{
Assert.That(CompleteRegistryKey.IsKeyPresent, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyNotProvided()
{
Assert.That(PartialRegistryKey.IsKeyPresent, Is.EqualTo(false));
}
#endregion
#region WindowsRegistryKeyBoolean tests
[Test]
public void WindowsRegistryKeyBoolean_FromStringTrue()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.String,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "true"
};
WindowsRegistryKeyBoolean boolKey = new ();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyBoolean_FromStringFalse()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.String,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "false"
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyBoolean_FromDwordTrue()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "1"
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyBoolean_FromDwordFalse()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "0"
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyBoolean_ReturnDefault()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = null
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey, true);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(false));
Assert.That(boolKey.Value, Is.EqualTo(true));
}
#endregion
#region WindowsRegistryKeyInteger tests
[Test]
public void WindowsRegistryKeyInteger()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestIntigerString",
Path = @"SOFTWARE\Test",
Value = "4711"
};
WindowsRegistryKeyInteger IntKey = new();
IntKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(IntKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(IntKey.Value, Is.EqualTo(4711));
}
[Test]
public void WindowsRegistryKeyInteger_ReturnDefault()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestIntigerString",
Path = @"SOFTWARE\Test",
Value = null
};
WindowsRegistryKeyInteger IntKey = new();
IntKey.ConvertFromWindowsRegistryKey(TestKey, 2096);
Assert.That(IntKey.IsKeyPresent, Is.EqualTo(false));
Assert.That(IntKey.Value, Is.EqualTo(2096));
}
#endregion
#region WindowsRegistryKeyString tests
[Test]
public void WindowsRegistryKeyString()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "The Big Bang Theory"
};
WindowsRegistryKeyString StrKey = new();
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.Value, Is.EqualTo("The Big Bang Theory"));
}
[Test]
public void WindowsRegistryKeyString_ReturnDefault()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = null
};
WindowsRegistryKeyString StrKey = new();
StrKey.ConvertFromWindowsRegistryKey(TestKey, "South Park");
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(false));
Assert.That(StrKey.Value, Is.EqualTo("South Park"));
}
[Test]
public void WindowsRegistryKeyString_ValidateSuccess()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "Big Bang"
};
WindowsRegistryKeyString StrKey = new()
{
AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" }
};
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccess()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "ig ang"
};
WindowsRegistryKeyString StrKey = new()
{
AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" }
};
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyString_ValidateSuccessCase()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "BiG BAng"
};
WindowsRegistryKeyString StrKey = new()
{
AllowedValues = new[] { "BiG BAng", "Big Bang Theory", "The Big Bang Theory" },
IsCaseSensitiveValidation = true
};
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccessCase()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "BiG BAng"
};
WindowsRegistryKeyString StrKey = new()
{
AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" },
IsCaseSensitiveValidation = true
};
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccessReturnNull()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "ig ang"
};
WindowsRegistryKeyString StrKey = new()
{
AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" }
};
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
Assert.That(StrKey.Value, Is.EqualTo(null));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccessValidValue()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "ig ang"
};
WindowsRegistryKeyString StrKey = new()
{
AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" }
};
StrKey.ConvertFromWindowsRegistryKey(TestKey, "Big Bang Theory");
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
Assert.That(StrKey.Value, Is.EqualTo("Big Bang Theory"));
}
#endregion
}
}