Class RegistryValues
This sealed class exposes static methods for efficiently testing for the presence of named values in a Registry key that behave more like the Item property of a collection, and retrieving Registry value types that require a transformation of one sort or another to get them into the appropriate native type.
Since static classes are implicitly sealed, this class cannot be inherited.
Inheritance
Namespace: WizardWrx.Core
Assembly: WizardWrx.Core.dll
Syntax
public static class RegistryValues : object
Fields
| Improve this Doc View SourceREG_BIANRY_EMPTY_ARRAY_FOR_ABSENT
Specify this constant as the value of RegQueryValue argument pfNullIsOK if your routine expects an array of some kind, even when the requested value is absent or the wrong type.
Declaration
public const bool REG_BIANRY_EMPTY_ARRAY_FOR_ABSENT = null
Field Value
Type | Description |
---|---|
System.Boolean |
REG_BINARY_NULL_FOR_ABSENT
Specify this constant as the value of RegQueryValue argument pfNullIsOK if your routine expects a null reference when the requested value is absent or the wrong type.
Declaration
public const bool REG_BINARY_NULL_FOR_ABSENT = null
Field Value
Type | Description |
---|---|
System.Boolean |
Methods
| Improve this Doc View SourceRegQueryValue(RegistryKey, String)
Return the multiple values stored in a REG_MULTI_SZ Registry value.
Declaration
public static string[] RegQueryValue(RegistryKey phRegKey, string pstrValueName)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | phRegKey | Pass in a handle to the Registry sub-key that is supposed to contain the required REG_MULTI_SZ value. |
System.String | pstrValueName | Pass in a string that contains the name of the value to be queried and returned if present. See notes in the Return Value comment. |
Returns
Type | Description |
---|---|
System.String[] | If the function succeeds, the return value is an array of strings that contains one string for each substring in the REG_MULTI_SZ value. If the value specified by pstrValueName is missing entirely, an empty array is returned. If the requested string is something other than REG_MULTI_SZ, this method treats the call as a degenerate case, returning an array of one element if the value is either REG_SZ or REG_EXPAND_SZ. ALL other types return empty arrays. |
RegQueryValue(RegistryKey, String, Boolean)
Get a REG_BINARY value from the Windows Registry.
Declaration
public static byte[] RegQueryValue(RegistryKey phRegKey, string pstrValueName, bool pfNullIsOK)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | phRegKey | Specify the handle of the Registry key where the value is expected to exist. This must be the key that holds the value, since neither RegistryKey.GetValueKind, nor RegistryKey.GetValue supports paths. |
System.String | pstrValueName | Specify the name, sans square brackets, which aren't needed, since the string is the unqualified name of a value. |
System.Boolean | pfNullIsOK | Specify True if your code tests the return value for a null reference. Otherwise, specify False, and expect an uninitialized one-element byte array. This is a specific instance in which I believe that a null reference is the best solution to the problem of what to use as a default for a missing REG_BINARY value. |
Returns
Type | Description |
---|---|
System.Byte[] | If the specified value exists and its type is REG_BINARY, the return value is a byte array containing the value read from the Registry. Otherwise, the return value is null (Nothing in Visual Basic). |
RegQueryValue(RegistryKey, String, Int32)
Get a REG_DWORD value from the Windows Registry.
Declaration
public static int RegQueryValue(RegistryKey phRegKey, string pstrValueName, int pintDefault)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | phRegKey | Specify the handle of the Registry key where the value is expected to exist. This must be the key that holds the value, since neither RegistryKey.GetValueKind, nor RegistryKey.GetValue supports paths. |
System.String | pstrValueName | Specify the name, sans square brackets, which aren't needed, since the string is the unqualified name of a value. |
System.Int32 | pintDefault | Although a DWORD seems logically more like an unsigned integer, I am somewhat surprised that the framework casts it to int, which carries a sign. Nevertheless, since the return is cast to int, so is its default, which is returned if the specified value is absent, or if it exists, but has a different type (ValueKind) property. |
Returns
Type | Description |
---|---|
System.Int32 | If the named value exists in the specified key and its type is REG_DWORD, it is returned, as a SIGNED integer. Otherwise, the value specified in pintDefault is returned. |
RegQueryValue(RegistryKey, String, Int64)
Get a REG_QWORD value from the Windows Registry.
Declaration
public static long RegQueryValue(RegistryKey phRegKey, string pstrValueName, long plngDefault)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | phRegKey | Specify the handle of the Registry key where the value is expected to exist. This must be the key that holds the value, since neither RegistryKey.GetValueKind, nor RegistryKey.GetValue supports paths. |
System.String | pstrValueName | Specify the name, sans square brackets, which aren't needed, since the string is the unqualified name of a value. |
System.Int64 | plngDefault | Although a QWORD seems logically more like an unsigned long, I am somewhat surprised that the framework casts it to long, which carries a sign. Nevertheless, since the return is cast to long, so is its default, which is returned if the specified value is absent, or if it exists, but has a different type (ValueKind) property. |
Returns
Type | Description |
---|---|
System.Int64 | If the named value exists in the specified key and its type is REG_QWORD, it is returned, as a SIGNED long integer. Otherwise, the value specified in plngDefault is returned. |
RegQueryValue(RegistryKey, String, String)
Return the single string representation of a REG_SZ or REG_EXPAND_SZ Registry value.
Declaration
public static string RegQueryValue(RegistryKey phRegKey, string pstrValueName, string pstrDefault)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | phRegKey | Pass in a handle to the Registry sub-key that is supposed to contain the required REG_SZ or REG_EXPAND_SZ value. |
System.String | pstrValueName | Pass in a string that contains the name of the value to be queried and returned if present. See notes in the Return Value comment. |
System.String | pstrDefault | Specify the string to return if the value is missing or of the wrong type. |
Returns
Type | Description |
---|---|
System.String | If the function succeeds, the return value is a string containing the value. If the value type is REG_EXPAND_SZ, environment variables are expanded. If the value specified by pstrValueName is missing entirely, the return value is pstrDefault. If the requested string is something other than REG_SZ or REG_EXPAND_SZ, the return value is pstrDefault. |
ValueExists(RegistryKey, String)
Return TRUE if a specified value exists on a specified key of the Windows Registry.
Declaration
public static bool ValueExists(RegistryKey phRegKey, string pstrValueName)
Parameters
Type | Name | Description |
---|---|---|
RegistryKey | phRegKey | Pass in a handle to the Registry sub-key that is supposed to contain the desired value. |
System.String | pstrValueName | Pass in a string that contains the name of the value to be queried and returned if present. See notes in the Return Value comment. |
Returns
Type | Description |
---|---|
System.Boolean | The function returns true if the key contains a value with the specified name, or if pstrValueName is the empty string or a null reference, either of which stands for the default value, which is always present, though seldom set. Otherwise, the return value is false. |
Remarks
This was written as a service routine for companion methods GetRegDwordValue and GetRegMultiStringValue. However, since it is almost certain to find use on its own, I marked it public.