From 33426ceee9cd0920f11a934304d0ca17920a9d96 Mon Sep 17 00:00:00 2001 From: Schmitti91 Date: Tue, 23 Jan 2024 14:29:07 +0100 Subject: [PATCH] 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. --- mRemoteNG/Tools/WindowsRegistry/IRegistry.cs | 2 ++ .../WindowsRegistry/IRegistryAdvanced.cs | 4 +++- .../WindowsRegistry/IRegistryAdvancedRead.cs | 4 +++- .../Tools/WindowsRegistry/IRegistryRead.cs | 3 ++- .../Tools/WindowsRegistry/WindowsRegistry.cs | 20 +++++++++++++++++++ .../Registry/WindowsRegistryAdvancedTests.cs | 7 +++++++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/mRemoteNG/Tools/WindowsRegistry/IRegistry.cs b/mRemoteNG/Tools/WindowsRegistry/IRegistry.cs index c79aeae3d..0bf2da86c 100644 --- a/mRemoteNG/Tools/WindowsRegistry/IRegistry.cs +++ b/mRemoteNG/Tools/WindowsRegistry/IRegistry.cs @@ -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); diff --git a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs b/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs index 6cab5cc52..649601105 100644 --- a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs +++ b/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs @@ -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); diff --git a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs b/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs index ac9df6744..8d865e179 100644 --- a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs +++ b/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs @@ -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); diff --git a/mRemoteNG/Tools/WindowsRegistry/IRegistryRead.cs b/mRemoteNG/Tools/WindowsRegistry/IRegistryRead.cs index 356ba368f..28b5d1423 100644 --- a/mRemoteNG/Tools/WindowsRegistry/IRegistryRead.cs +++ b/mRemoteNG/Tools/WindowsRegistry/IRegistryRead.cs @@ -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); diff --git a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs index 9899adf7c..954bc415a 100644 --- a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs +++ b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs @@ -100,6 +100,26 @@ namespace mRemoteNG.Tools.WindowsRegistry return defaultValue; } + // + /// Retrieves a DWORD value from the Windows Registry, with an optional default value. + /// + /// The Registry hive to access. + /// The Registry path containing the key. + /// The name of the Registry property. + /// The default value to return if the property is not found or cannot be parsed. + /// The DWORD value from the Registry, or the specified default value. + 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; + } + /// /// Retrieves a WindowsRegistryKey object for a specific registry hive, path, and value name. /// diff --git a/mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs b/mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs index 820a11b32..093822354 100644 --- a/mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs +++ b/mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs @@ -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()