Visual Studio
Registry Look-Ups

Some build tasks require the input of paths and other information that is contained in registry keys or in environment variables. A build configuration file, such as msbuild.exe.config, can read these values directly from the registry,

The $(registry:xxxx) syntax can be used anywhere in the project where property expressions are enabled. In such cases, if there is an error, an "invalid project file" error message occurs that explains the problem.

Reading Registry Values

The following property definition reads a value from a registry key:

<PropertyGroup>
  <MyProperty>$(registry:HKEY_LOCAL_MACHINE\MyKey\MySubKey@ValueName)</MyProperty>
</PropertyGroup>

In this example, ValueName represents the value in the \HKEY_LOCAL_MACHINE\MyKey\MySubKey registry key. To read the default value, reference \HKEY_LOCAL_MACHINE\MyKey\MySubKey without specifying <ValueName>. For example:

<PropertyGroup>
  <MyProperty>$(registry:HKEY_LOCAL_MACHINE\MyKey\MySubKey)</MyProperty>
</PropertyGroup>
NoteNote:

If you try to read the registry key and you do not have the required user rights, an access violation may occur and you may receive an error that is similar to the following.

MSBUILD: Configuration error MSB4146: Cannot evaluate the property expression "..\WinFx\v3.5$(registry:HKEY_LOCAL_MACHINE\software\x)" found at "c:\ToolPlat\src\tools\x86\managed\v2.0\MSBuild.exe.config, line 15". The expression "$(registry:HKEY_LOCAL_MACHINE\software\x)" cannot be evaluated. Requested registry access is not allowed.

See Also

Concepts

Tags :


Community Content

Thomas Lee
Typo corrections

There are various errors in every single example in the above text (e.g. a missing closing parenthesis, a missing "@" symbol, or a missing "registry:" prefix). The correct syntax for referencing a registry item is:

$(registry:HKEY_LOCAL_MACHINE\MyKey\MySubKey@ValueName)

-Gonz

Tags : contentbug

Thomas Lee
Another typo
"A similar error occurs if the expression is in a project rather than the config file. "
This is not true. This syntax in fact is typically used in project files.
Tags : contentbug

Page view tracker