VS2010: How to use Environment Variables in Post-Build

The '%' character is reserved by MSBuild, so you have to replace it by the %25 hexadecimal escape sequence as documented in MSDN.

copy "$(TargetDir)$(TargetName).*" "%253DSMaxInstallDirectory%25\Plugins" should actually work. However: Visual Studio's commandline editor displays it correctly, but MSBuild then interprets %253 wrongly. I can't tell whether it's a bug or a feature but you must not start your environment variable's name with a digit.


Visual studio doesn't properly encode/decode "special" characters in the XML config file. I'm able to get it to work as expected by manually escaping the command as a URL (http://www.w3schools.com/tags/ref_urlencode.asp).

ECHO %CD%

Results in an output log:

Target "PreBuildEvent" in file "blahblahblah"
    Task "Exec"
        Command:
        ECHO %CD%
        C:\blah\blah\blah\Debug
    Done executing task "Exec".

However, using URL escapes in the project proerties dialog appears to work:

ECHO %25CD%25

This works for me in the Post-Build setting of the 'Build Events' of the project.

echo %CodeContractsInstallDir%
echo %DXSDK_DIR%
echo "%ONLYME%"

ONLYME is a environment var in the User variables of my profile.

The others are System wide vars.

ONLYME stays empty if I start VS2010 as administrator, the systemvars still have values as expected.

I'm on V2010 SP1