// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.CmdPal.Ext.Registry.Helpers;
using Microsoft.Win32;
namespace Microsoft.CmdPal.Ext.Registry.Classes;
///
/// A entry of the registry.
///
internal sealed class RegistryEntry
{
#pragma warning disable CS8632
///
/// Gets the full path to a registry key.
///
internal string KeyPath { get; }
///
/// Gets the registry key for this entry.
///
internal RegistryKey? Key { get; }
///
/// Gets a possible exception that was occurred when try to open this registry key (e.g. ).
///
internal Exception? Exception { get; }
///
/// Gets the name of the current selected registry value.
///
internal string? ValueName { get; }
///
/// Gets the value of the current selected registry value.
///
internal object? ValueData { get; }
#pragma warning restore CS8632
///
/// Initializes a new instance of the class.
///
/// The full path to the registry key for this entry.
/// A exception that was occurred when try to access this registry key.
internal RegistryEntry(string keyPath, Exception exception)
{
KeyPath = keyPath;
Exception = exception;
}
///
/// Initializes a new instance of the class.
///
/// The for this entry.
internal RegistryEntry(RegistryKey key)
{
KeyPath = key.Name;
Key = key;
}
///
/// Initializes a new instance of the class.
///
/// The for this entry.
/// The value name of the current selected registry value.
/// The value of the current selected registry value.
internal RegistryEntry(RegistryKey key, string valueName, object value)
{
KeyPath = key.Name;
Key = key;
ValueName = valueName;
ValueData = value;
}
///
/// Return the registry key.
///
/// A registry key.
internal string GetRegistryKey()
{
return $"{Key?.Name ?? KeyPath}";
}
///
/// Return the value name with the complete registry key.
///
/// A value name with the complete registry key.
internal string GetValueNameWithKey()
{
return $"{Key?.Name ?? KeyPath}\\\\{ValueName?.ToString() ?? string.Empty}";
}
///
/// Return the value data of a value name inside a registry key.
///
/// A value data.
internal string GetValueData()
{
if (Key is null)
{
return KeyPath;
}
if (string.IsNullOrEmpty(ValueName))
{
return Key.Name;
}
return ValueHelper.GetValue(Key, ValueName);
}
}