Add GetDwordValue method to WindowsRegistryAdvanced

This commit introduces the GetDwordValue method in the WindowsRegistryAdvanced class, allowing the retrieval of DWORD values from the Windows Registry. The implementation uses int.TryParse for efficient parsing and handles default values gracefully.
This commit is contained in:
Schmitti91
2024-01-23 14:29:07 +01:00
parent 5f5700b948
commit 33426ceee9
6 changed files with 37 additions and 3 deletions

View File

@@ -15,7 +15,9 @@ namespace mRemoteNG.Tools.WindowsRegistry
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);

View File

@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using static mRemoteNG.Config.Settings.Registry.RegistryController;
namespace mRemoteNG.Tools.WindowsRegistry
{
@@ -17,6 +16,9 @@ namespace mRemoteNG.Tools.WindowsRegistry
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);

View File

@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using static mRemoteNG.Config.Settings.Registry.RegistryController;
namespace mRemoteNG.Tools.WindowsRegistry
{
@@ -17,6 +16,9 @@ namespace mRemoteNG.Tools.WindowsRegistry
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);

View File

@@ -13,10 +13,11 @@ namespace mRemoteNG.Tools.WindowsRegistry
{
#region registry 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);

View File

@@ -100,6 +100,26 @@ namespace mRemoteNG.Tools.WindowsRegistry
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)
{
var 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>

View File

@@ -113,6 +113,13 @@ namespace mRemoteNGTests.Tools.Registry
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()