Skip to content

Commit

Permalink
Add support for multiple registry views.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewKing committed Nov 7, 2023
1 parent 341f4e7 commit 45110bb
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/DeviceId.Windows/Components/RegistryValueDeviceIdComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;

namespace DeviceId.Windows.Components;
Expand All @@ -10,14 +12,14 @@ public class RegistryValueDeviceIdComponent : IDeviceIdComponent
{
#if !NET35
/// <summary>
/// The registry hive.
/// The registry views.
/// </summary>
private readonly RegistryHive _registryHive;
private readonly RegistryView[] _registryViews;

/// <summary>
/// The registry views.
/// The registry hive.
/// </summary>
private readonly RegistryView _registryView;
private readonly RegistryHive _registryHive;
#endif

/// <summary>
Expand Down Expand Up @@ -76,9 +78,20 @@ public RegistryValueDeviceIdComponent(RegistryView registryView, RegistryHive re
/// <param name="valueName">The name of the registry value.</param>
/// <param name="formatter">An optional function to use to format the value before returning it.</param>
public RegistryValueDeviceIdComponent(RegistryView registryView, RegistryHive registryHive, string keyName, string valueName, Func<string, string> formatter)
: this(new RegistryView[] { registryView }, registryHive, keyName, valueName, formatter) { }

/// <summary>
/// Initializes a new instance of the <see cref="RegistryValueDeviceIdComponent"/> class.
/// </summary>
/// <param name="registryViews">The registry views.</param>
/// <param name="registryHive">The registry hive.</param>
/// <param name="keyName">The name of the registry key.</param>
/// <param name="valueName">The name of the registry value.</param>
/// <param name="formatter">An optional function to use to format the value before returning it.</param>
public RegistryValueDeviceIdComponent(IEnumerable<RegistryView> registryViews, RegistryHive registryHive, string keyName, string valueName, Func<string, string> formatter)
{
_registryViews = registryViews.ToArray();
_registryHive = registryHive;
_registryView = registryView;
_keyName = keyName;
_valueName = valueName;
_formatter = formatter;
Expand All @@ -92,6 +105,10 @@ public RegistryValueDeviceIdComponent(RegistryView registryView, RegistryHive re
public string GetValue()
{
#if NET35
// In .NET 3.5, it's not possible to specify the registry view.
// Technically I could write some native API calls to do it properly,
// but we're going to drop support for .NET 3.5 soon anyway, so I don't really want to bother.

try
{
var value = Registry.GetValue(_keyName, _valueName, null);
Expand All @@ -105,23 +122,24 @@ public string GetValue()
}
catch { }
#else
try
foreach (var registryView in _registryViews)
{
using var registry = RegistryKey.OpenBaseKey(_registryHive, _registryView);
using var subKey = registry.OpenSubKey(_keyName);
if (subKey != null)
try
{
var value = subKey.GetValue(_valueName);
var valueAsString = value?.ToString();
if (valueAsString is null)
using var registry = RegistryKey.OpenBaseKey(_registryHive, registryView);
using var subKey = registry.OpenSubKey(_keyName);
if (subKey != null)
{
return null;
var value = subKey.GetValue(_valueName);
var valueAsString = value?.ToString();
if (valueAsString != null)
{
return _formatter?.Invoke(valueAsString) ?? valueAsString;
}
}

return _formatter?.Invoke(valueAsString) ?? valueAsString;
}
catch { }
}
catch { }
#endif

return null;
Expand Down

0 comments on commit 45110bb

Please sign in to comment.