Class RegistryUtils
Provides utility and extension methods for Registry access.
public static class RegistryUtils
- Inheritance
-
objectRegistryUtils
Methods
CreateSubKeyChecked(RegistryKey, string)
Creates a registry key with retries and mapping System.Security.SecurityExceptions to System.UnauthorizedAccessExceptions.
[MustDisposeResource]
public static RegistryKey CreateSubKeyChecked(this RegistryKey key, string subkeyName)
Parameters
keyRegistryKeyThe key to create a subkey in.
subkeyNamestringThe name of the subkey to create.
Returns
- RegistryKey
The newly created subkey.
Exceptions
- IOException
Failed to create the key.
- UnauthorizedAccessException
Write access to the key is not permitted.
DeleteSoftwareValue(string, string, bool)
Deletes a value from one of the SOFTWARE keys in the registry.
public static void DeleteSoftwareValue(string subkeyName, string valueName, bool machineWide)
Parameters
subkeyNamestringThe path of the key relative to the SOFTWARE key.
valueNamestringThe name of the value to delete.
machineWidebooltrueto delete from HKLM/SOFTWARE (and HKLM/SOFTWARE/Wow6432Node if on 64-bit Windows);falseto delete from HCKU/SOFTWARE.
Remarks
Does not throw an exception for missing keys or values.
GetDword(string, string?, int)
Reads a DWORD value from the registry.
[Pure]
public static int GetDword(string keyName, string? valueName, int defaultValue = 0)
Parameters
keyNamestringThe full path of the key to read from.
valueNamestringThe name of the value to read.
defaultValueintThe default value to return if the key or value does not exist.
Returns
- int
Exceptions
- UnauthorizedAccessException
Read access to the key is not permitted.
GetLastWriteTime(RegistryKey)
Returns the last write time of the registry key.
public static DateTime GetLastWriteTime(this RegistryKey key)
Parameters
keyRegistryKeyThe key to get the last write time for.
Returns
- DateTime
Exceptions
- IOException
The key does not exist.
- UnauthorizedAccessException
Read access to the key is not permitted.
GetSoftwareString(string, string?, bool)
Reads a string value from one of the SOFTWARE keys in the registry.
[Pure]
public static string? GetSoftwareString(string subkeyName, string? valueName, bool machineWide)
Parameters
subkeyNamestringThe path of the key relative to the SOFTWARE key.
valueNamestringThe name of the value to read.
machineWidebooltrueto read from HKLM/SOFTWARE (and HKLM/SOFTWARE/Wow6432Node if on 64-bit Windows);falseto read from HCKU/SOFTWARE.
Returns
- string
GetString(string, string?, string?)
Reads a string value from the registry.
[Pure]
public static string? GetString(string keyName, string? valueName, string? defaultValue = null)
Parameters
keyNamestringThe full path of the key to read from.
valueNamestringThe name of the value to read.
defaultValuestringThe default value to return if the key or value does not exist.
Returns
- string
Exceptions
- UnauthorizedAccessException
Read access to the key is not permitted.
GetSubKeyNames(RegistryKey, string)
Retrieves the names of all subkeys within a specific subkey of a registry root.
[Pure]
public static string[] GetSubKeyNames(RegistryKey key, string subkeyName)
Parameters
keyRegistryKeyThe root key to look within.
subkeyNamestringThe path of the subkey below
key.
Returns
- string[]
A list of key names; an empty array if the key does not exist.
GetValueNames(RegistryKey, string)
Retrieves the names of all values within a specific subkey of a registry root.
[Pure]
public static string[] GetValueNames(this RegistryKey key, string subkeyName)
Parameters
keyRegistryKeyThe root key to look within.
subkeyNamestringThe path of the subkey below
key.
Returns
- string[]
A list of value names; an empty array if the key does not exist.
OpenHklmKey(string, out bool)
Opens a HKEY_LOCAL_MACHINE key in the registry for reading, first trying to find the 64-bit version of it, then falling back to the 32-bit version.
[MustDisposeResource]
public static RegistryKey OpenHklmKey(string subkeyName, out bool x64)
Parameters
subkeyNamestringThe path to the key below HKEY_LOCAL_MACHINE.
x64boolIndicates whether a 64-bit key was opened.
Returns
- RegistryKey
The opened registry key.
Exceptions
- IOException
The key does not exist.
- UnauthorizedAccessException
Read access to the key is not permitted.
OpenSubKeyChecked(RegistryKey, string, bool)
Opens a registry key with retries and mapping System.Security.SecurityExceptions to System.UnauthorizedAccessExceptions.
[MustDisposeResource]
public static RegistryKey OpenSubKeyChecked(this RegistryKey key, string subkeyName, bool writable = false)
Parameters
keyRegistryKeyThe key to open a subkey in.
subkeyNamestringThe name of the subkey to open.
writablebooltruefor write-access to the key.
Returns
- RegistryKey
The opened subkey.
Exceptions
- IOException
The key does not exist.
- UnauthorizedAccessException
The requested access to the key is not permitted.
SetDword(string, string?, int)
Sets a DWORD value in the registry.
public static void SetDword(string keyName, string? valueName, int value)
Parameters
keyNamestringThe full path of the key to write to.
valueNamestringThe name of the value to write.
valueintThe value to write.
Exceptions
- UnauthorizedAccessException
Write access to the key is not permitted.
SetSoftwareString(string, string?, string, bool)
Sets a string value in one or more of the SOFTWARE keys in the registry.
public static void SetSoftwareString(string subkeyName, string? valueName, string value, bool machineWide = false)
Parameters
subkeyNamestringThe path of the key relative to the SOFTWARE key.
valueNamestringThe name of the value to write.
valuestringThe value to write.
machineWidebooltrueto write to HKLM/SOFTWARE (and HKLM/SOFTWARE/Wow6432Node if on 64-bit Windows);falseto write to HCKU/SOFTWARE.
Exceptions
- UnauthorizedAccessException
Write access to the key is not permitted.
SetString(string, string?, string)
Sets a string value in the registry.
public static void SetString(string keyName, string? valueName, string value)
Parameters
keyNamestringThe full path of the key to write to.
valueNamestringThe name of the value to write.
valuestringThe value to write.
Exceptions
- UnauthorizedAccessException
Write access to the key is not permitted.
TryDeleteSubKey(RegistryKey, string)
Recursively deletes a registry key. Does not throw exceptions.
public static bool TryDeleteSubKey(this RegistryKey key, string subkeyName)
Parameters
keyRegistryKeyThe key to containing the subkey to delete.
subkeyNamestringThe name of the subkey to delete.
Returns
- bool
trueif the key was deleted or didn't exist;falseif the deletion failed.
TryOpenSubKey(RegistryKey, string, bool)
Trys to open a registry key with retries and mapping System.Security.SecurityExceptions to System.UnauthorizedAccessExceptions.
[MustDisposeResource]
public static RegistryKey? TryOpenSubKey(this RegistryKey key, string subkeyName, bool writable = false)
Parameters
keyRegistryKeyThe key to open a subkey in.
subkeyNamestringThe name of the subkey to open.
writablebooltruefor write-access to the key.
Returns
- RegistryKey
The opened subkey;
nullif it does not exist.
Exceptions
- UnauthorizedAccessException
The requested access to the key is not permitted.