Class RegistryUtils
Provides utility and extension methods for Registry access.
Namespace: NanoByte.Common.Native
Assembly: NanoByte.Common.dll
Syntax
public static class RegistryUtils : Object
Methods
CreateSubKeyChecked(RegistryKey, String)
Creates a registry key with retries and mapping SecurityExceptions to UnauthorizedAccessExceptions.
Declaration
public static RegistryKey CreateSubKeyChecked(this RegistryKey key, string subkeyName)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | key | The key to create a subkey in. |
String | subkeyName | The name of the subkey to create. |
Returns
Type | Description |
---|---|
RegistryKey | The newly created subkey. |
Exceptions
Type | Condition |
---|---|
IOException | Failed to create the key. |
UnauthorizedAccessException | Write access to the key is not permitted. |
DeleteSoftwareValue(String, String, Boolean)
Deletes a value from one of the SOFTWARE keys in the registry.
Declaration
public static void DeleteSoftwareValue(string subkeyName, string valueName, bool machineWide)
Parameters
Type | Name | Description |
---|---|---|
String | subkeyName | The path of the key relative to the SOFTWARE key. |
String | valueName | The name of the value to delete. |
Boolean | machineWide |
|
Remarks
Does not throw an exception for missing keys or values.
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
GetDword(String, String, Int32)
Reads a DWORD value from the registry.
Declaration
public static int GetDword(string keyName, string valueName, int defaultValue = 0)
Parameters
Type | Name | Description |
---|---|---|
String | keyName | The full path of the key to read from. |
String | valueName | The name of the value to read. |
Int32 | defaultValue | The default value to return if the key or value does not exist. |
Returns
Type | Description |
---|---|
Int32 |
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
UnauthorizedAccessException | Read access to the key is not permitted. |
GetSoftwareString(String, String, Boolean)
Reads a string value from one of the SOFTWARE keys in the registry.
Declaration
public static string GetSoftwareString(string subkeyName, string valueName, bool machineWide)
Parameters
Type | Name | Description |
---|---|---|
String | subkeyName | The path of the key relative to the SOFTWARE key. |
String | valueName | The name of the value to read. |
Boolean | machineWide |
|
Returns
Type | Description |
---|---|
String |
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
GetSoftwareString(String, String, String)
Reads a string value from one of the SOFTWARE keys in the registry.
Declaration
public static string GetSoftwareString(string subkeyName, string valueName, string defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
String | subkeyName | The path of the key relative to the SOFTWARE key. |
String | valueName | The name of the value to read. |
String | defaultValue | The default value to return if the key or value does not exist. |
Returns
Type | Description |
---|---|
String |
Remarks
Checks HKLM/SOFTWARE, HKLM/SOFTWARE/Wow6432Node and HKCU/SOFTWARE in that order.
Exceptions
Type | Condition |
---|---|
IOException | Registry access failed. |
GetString(String, String, String)
Reads a string value from the registry.
Declaration
public static string GetString(string keyName, string valueName, string defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
String | keyName | The full path of the key to read from. |
String | valueName | The name of the value to read. |
String | defaultValue | The default value to return if the key or value does not exist. |
Returns
Type | Description |
---|---|
String |
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
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.
Declaration
public static string[] GetSubKeyNames(RegistryKey key, string subkeyName)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | key | The root key to look within. |
String | subkeyName | The path of the subkey below |
Returns
Type | Description |
---|---|
String[] | A list of key names; an empty array if the key does not exist. |
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
GetValueNames(RegistryKey, String)
Retrieves the names of all values within a specific subkey of a registry root.
Declaration
public static string[] GetValueNames(this RegistryKey key, string subkeyName)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | key | The root key to look within. |
String | subkeyName | The path of the subkey below |
Returns
Type | Description |
---|---|
String[] | A list of value names; an empty array if the key does not exist. |
OpenHklmKey(String, out Boolean)
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.
Declaration
public static RegistryKey OpenHklmKey(string subkeyName, out bool x64)
Parameters
Type | Name | Description |
---|---|---|
String | subkeyName | The path to the key below HKEY_LOCAL_MACHINE. |
Boolean | x64 | Indicates whether a 64-bit key was opened. |
Returns
Type | Description |
---|---|
RegistryKey | The opened registry key. |
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
UnauthorizedAccessException | Read access to the key is not permitted. |
OpenSubKeyChecked(RegistryKey, String, Boolean)
Opens a registry key with retries and mapping SecurityExceptions to UnauthorizedAccessExceptions.
Declaration
public static RegistryKey OpenSubKeyChecked(this RegistryKey key, string subkeyName, bool writable = false)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | key | The key to open a subkey in. |
String | subkeyName | The name of the subkey to open. |
Boolean | writable |
|
Returns
Type | Description |
---|---|
RegistryKey | The opened subkey. |
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
UnauthorizedAccessException | The requested access to the key is not permitted. |
SetDword(String, String, Int32)
Sets a DWORD value in the registry.
Declaration
public static void SetDword(string keyName, string valueName, int value)
Parameters
Type | Name | Description |
---|---|---|
String | keyName | The full path of the key to write to. |
String | valueName | The name of the value to write. |
Int32 | value | The value to write. |
Exceptions
Type | Condition |
---|---|
UnauthorizedAccessException | Write access to the key is not permitted. |
SetSoftwareString(String, String, String, Boolean)
Sets a string value in one or more of the SOFTWARE keys in the registry.
Declaration
public static void SetSoftwareString(string subkeyName, string valueName, string value, bool machineWide = false)
Parameters
Type | Name | Description |
---|---|---|
String | subkeyName | The path of the key relative to the SOFTWARE key. |
String | valueName | The name of the value to write. |
String | value | The value to write. |
Boolean | machineWide |
|
Exceptions
Type | Condition |
---|---|
IOException | The key does not exist. |
UnauthorizedAccessException | Write access to the key is not permitted. |
SetString(String, String, String)
Sets a string value in the registry.
Declaration
public static void SetString(string keyName, string valueName, string value)
Parameters
Type | Name | Description |
---|---|---|
String | keyName | The full path of the key to write to. |
String | valueName | The name of the value to write. |
String | value | The value to write. |
Exceptions
Type | Condition |
---|---|
UnauthorizedAccessException | Write access to the key is not permitted. |
TryOpenSubKey(RegistryKey, String, Boolean)
Trys to open a registry key with retries and mapping SecurityExceptions to UnauthorizedAccessExceptions.
Declaration
public static RegistryKey TryOpenSubKey(this RegistryKey key, string subkeyName, bool writable = false)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | key | The key to open a subkey in. |
String | subkeyName | The name of the subkey to open. |
Boolean | writable |
|
Returns
Type | Description |
---|---|
RegistryKey | The opened subkey; |
Exceptions
Type | Condition |
---|---|
UnauthorizedAccessException | The requested access to the key is not permitted. |