Class RegistryUtils
Provides utility and extension methods for Registry access.
public static class RegistryUtils
- Inheritance
-
RegistryUtils
Methods
CreateSubKeyChecked(RegistryKey, string)
Creates a registry key with retries and mapping SecurityExceptions to UnauthorizedAccessExceptions.
[MustDisposeResource]
public static RegistryKey CreateSubKeyChecked(this RegistryKey key, string subkeyName)
Parameters
key
RegistryKeyThe key to create a subkey in.
subkeyName
stringThe 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
subkeyName
stringThe path of the key relative to the SOFTWARE key.
valueName
stringThe name of the value to delete.
machineWide
booltrue
to delete from HKLM/SOFTWARE (and HKLM/SOFTWARE/Wow6432Node if on 64-bit Windows);false
to 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
keyName
stringThe full path of the key to read from.
valueName
stringThe name of the value to read.
defaultValue
intThe default value to return if the key or value does not exist.
Returns
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
key
RegistryKeyThe key to get the last write time for.
Returns
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
subkeyName
stringThe path of the key relative to the SOFTWARE key.
valueName
stringThe name of the value to read.
machineWide
booltrue
to read from HKLM/SOFTWARE (and HKLM/SOFTWARE/Wow6432Node if on 64-bit Windows);false
to read from HCKU/SOFTWARE.
Returns
GetString(string, string?, string?)
Reads a string value from the registry.
[Pure]
public static string? GetString(string keyName, string? valueName, string? defaultValue = null)
Parameters
keyName
stringThe full path of the key to read from.
valueName
stringThe name of the value to read.
defaultValue
stringThe default value to return if the key or value does not exist.
Returns
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
key
RegistryKeyThe root key to look within.
subkeyName
stringThe 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
key
RegistryKeyThe root key to look within.
subkeyName
stringThe 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
subkeyName
stringThe path to the key below HKEY_LOCAL_MACHINE.
x64
boolIndicates 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 SecurityExceptions to UnauthorizedAccessExceptions.
[MustDisposeResource]
public static RegistryKey OpenSubKeyChecked(this RegistryKey key, string subkeyName, bool writable = false)
Parameters
key
RegistryKeyThe key to open a subkey in.
subkeyName
stringThe name of the subkey to open.
writable
booltrue
for 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
keyName
stringThe full path of the key to write to.
valueName
stringThe name of the value to write.
value
intThe 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
subkeyName
stringThe path of the key relative to the SOFTWARE key.
valueName
stringThe name of the value to write.
value
stringThe value to write.
machineWide
booltrue
to write to HKLM/SOFTWARE (and HKLM/SOFTWARE/Wow6432Node if on 64-bit Windows);false
to 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
keyName
stringThe full path of the key to write to.
valueName
stringThe name of the value to write.
value
stringThe 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
key
RegistryKeyThe key to containing the subkey to delete.
subkeyName
stringThe name of the subkey to delete.
Returns
- bool
true
if the key was deleted or didn't exist;false
if the deletion failed.
TryOpenSubKey(RegistryKey, string, bool)
Trys to open a registry key with retries and mapping SecurityExceptions to UnauthorizedAccessExceptions.
[MustDisposeResource]
public static RegistryKey? TryOpenSubKey(this RegistryKey key, string subkeyName, bool writable = false)
Parameters
key
RegistryKeyThe key to open a subkey in.
subkeyName
stringThe name of the subkey to open.
writable
booltrue
for write-access to the key.
Returns
- RegistryKey
The opened subkey;
null
if it does not exist.
Exceptions
- UnauthorizedAccessException
The requested access to the key is not permitted.