Table of Contents

Class RegistryUtils

Namespace
NanoByte.Common.Native
Assembly
NanoByte.Common.dll

Provides utility and extension methods for Registry access.

public static class RegistryUtils
Inheritance
object
RegistryUtils

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

key RegistryKey

The key to create a subkey in.

subkeyName string

The 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 string

The path of the key relative to the SOFTWARE key.

valueName string

The name of the value to delete.

machineWide bool

true 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 string

The full path of the key to read from.

valueName string

The name of the value to read.

defaultValue int

The 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

key RegistryKey

The 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

subkeyName string

The path of the key relative to the SOFTWARE key.

valueName string

The name of the value to read.

machineWide bool

true to read from HKLM/SOFTWARE (and HKLM/SOFTWARE/Wow6432Node if on 64-bit Windows); false to 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

keyName string

The full path of the key to read from.

valueName string

The name of the value to read.

defaultValue string

The 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

key RegistryKey

The root key to look within.

subkeyName string

The 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 RegistryKey

The root key to look within.

subkeyName string

The 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 string

The path to the key below HKEY_LOCAL_MACHINE.

x64 bool

Indicates 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

key RegistryKey

The key to open a subkey in.

subkeyName string

The name of the subkey to open.

writable bool

true 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 string

The full path of the key to write to.

valueName string

The name of the value to write.

value int

The 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 string

The path of the key relative to the SOFTWARE key.

valueName string

The name of the value to write.

value string

The value to write.

machineWide bool

true 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 string

The full path of the key to write to.

valueName string

The name of the value to write.

value string

The 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 RegistryKey

The key to containing the subkey to delete.

subkeyName string

The 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 System.Security.SecurityExceptions to System.UnauthorizedAccessExceptions.

[MustDisposeResource]
public static RegistryKey? TryOpenSubKey(this RegistryKey key, string subkeyName, bool writable = false)

Parameters

key RegistryKey

The key to open a subkey in.

subkeyName string

The name of the subkey to open.

writable bool

true 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.