RegistryKey::GetValue Method (String^, Object^)
Retrieves the value associated with the specified name. If the name is not found, returns the default value that you provide.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
-
Type:
System::String^
The name of the value to retrieve. This string is not case-sensitive.
- defaultValue
-
Type:
System::Object^
The value to return if name does not exist.
Return Value
Type: System::Object^The value associated with name, with any embedded environment variables left unexpanded, or defaultValue if name is not found.
| Exception | Condition |
|---|---|
| SecurityException | The user does not have the permissions required to read from the registry key. |
| ObjectDisposedException | The RegistryKey that contains the specified value is closed (closed keys cannot be accessed). |
| IOException | The RegistryKey that contains the specified value has been marked for deletion. |
| UnauthorizedAccessException | The user does not have the necessary registry rights. |
Use this overload of GetValue to handle the case where a name does not exist yet — for example, the first time your application is run. Whenever you call this overload, use the defaultValue parameter to specify the value to return if name does not exist.
Note |
|---|
A registry key can have one value that is not associated with any name. When this unnamed value is displayed in the registry editor, the string "(Default)" appears instead of a name. To retrieve this unnamed value, specify either null or the empty string ("") for name. |
When the GetValue method retrieves expandable string values (RegistryValueKind::ExpandString), it expands environment strings using data from the local environment. To retrieve expandable string values from the registry on a remote computer, use the GetValue overload to specify that you do not want environment strings expanded.
Note |
|---|
If a value containing expandable references to environment variables has been stored as a string (RegistryValueKind::String), rather than as an expandable string (RegistryValueKind::ExpandString), the GetValue method does not expand it. You can expand such a string after it has been retrieved by calling the ExpandEnvironmentVariables method. |
Note |
|---|
The recommended way to retrieve data from the PerformanceData key is to use the PerformanceCounter class rather than the RegistryKey::GetValue method. GetValue does not support reading values of type REG_NONE or REG_LINK. In both cases, the default value (null) is returned instead of the actual value. |
The following code example creates a test key with a value and retrieves that value. The example then attempts to retrieve a nonexistent value from the key; in this case the GetValue method returns the specified default value.
using namespace System; using namespace Microsoft::Win32; public ref 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: for each (Byte^ b in (array<Byte^>^)o) { Console::Write("{0:x2} ", b); } Console::WriteLine(); break; case RegistryValueKind::DWord: Console::WriteLine("Value = " + Convert::ToString((Int32^)o)); break; case RegistryValueKind::QWord: Console::WriteLine("Value = " + Convert::ToString((Int64^)o)); break; case RegistryValueKind::MultiString: for each (String^ s in (array<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(); } }; int main() { RegGetDef::Main(); } /* Output: Retrieving registry value ... Object Type = System.String Value = testData The default to return */
to read from the registry. Associated enumeration: RegistryPermissionAccess::Read
to read a registry key of type REG_EXPAND_SZ. Associated enumeration: PermissionState::Unrestricted
Available since 1.1
