Edit

Share via


RegistryValueKind Enum

Definition

Specifies the data types to use when storing values in the registry, or identifies the data type of a value in the registry.

C#
public enum RegistryValueKind
C#
[System.Runtime.InteropServices.ComVisible(true)]
public enum RegistryValueKind
Inheritance
RegistryValueKind
Attributes

Fields

Name Value Description
None -1

No data type.

Unknown 0

An unsupported registry data type. For example, the Microsoft Windows API registry data type REG_RESOURCE_LIST is unsupported. Use this value to specify that the SetValue(String, Object) method should determine the appropriate registry data type when storing a name/value pair.

String 1

A null-terminated string. This value is equivalent to the Windows API registry data type REG_SZ.

ExpandString 2

A null-terminated string that contains unexpanded references to environment variables, such as %PATH%, that are expanded when the value is retrieved. This value is equivalent to the Windows API registry data type REG_EXPAND_SZ.

Binary 3

Binary data in any form. This value is equivalent to the Windows API registry data type REG_BINARY.

DWord 4

A 32-bit binary number. This value is equivalent to the Windows API registry data type REG_DWORD.

MultiString 7

An array of null-terminated strings, terminated by two null characters. This value is equivalent to the Windows API registry data type REG_MULTI_SZ.

QWord 11

A 64-bit binary number. This value is equivalent to the Windows API registry data type REG_QWORD.

Examples

The following code example creates a registry key and sets several values for that key, using RegistryValueKind to specify the registry data types. The example then uses RegistryKey.GetValueKind to check the registry data types, in order to retrieve the values and display them.

C#
using System;
using Microsoft.Win32;

class RegGetDef
{
    public static void Main()
    {
        // Create a reference to a valid key.  In order for this code to
        // work, the indicated key must have been created previously.
        // The key name is not case-sensitive.
        RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\myTestKey", false);
        // Get the value from the specified name/value pair in the key.

        string valueName = "myTestValue";

        Console.WriteLine("Retrieving registry value ...");
        Console.WriteLine();
        object o = rk.GetValue(valueName);
        Console.WriteLine("Object Type = " + o.GetType().FullName);
        Console.WriteLine();
        switch (rk.GetValueKind(valueName))
        {
            case RegistryValueKind.String:
            case RegistryValueKind.ExpandString:
                Console.WriteLine("Value = " + o);
                break;
            case RegistryValueKind.Binary:
                foreach (byte b in (byte[])o)
                {
                    Console.Write("{0:x2} ", b);
                }
                Console.WriteLine();
                break;
            case RegistryValueKind.DWord:
                Console.WriteLine("Value = " + Convert.ToString((int)o));
                break;
            case RegistryValueKind.QWord:
                Console.WriteLine("Value = " + Convert.ToString((Int64)o));
                break;
            case RegistryValueKind.MultiString:
                foreach (string s in (string[])o)
                {
                    Console.Write("[{0:s}], ", s);
                }
                Console.WriteLine();
                break;
            default:
                Console.WriteLine("Value = (Unknown)");
                break;
        }

        // Attempt to retrieve a value that does not exist; the specified
        // default value is returned.
        string def = (string)rk.GetValue("notavalue", "The default to return");
        Console.WriteLine();
        Console.WriteLine(def);

        rk.Close();
    }
}
/*
Output:
Retrieving registry value ...

Object Type = System.String

Value = testData

The default to return
*/

Remarks

The RegistryValueKind enumeration defines the set of supported registry data types and the value that is used for unsupported types (Unknown). Starting in the .NET Framework 4, you can specify not to use a data type with the None value.

Use the RegistryKey.GetValueKind method to determine the data type of a registry key value before retrieving the value. When you set a registry key value, use the SetValue method to specify the registry data type explicitly.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

See also