13

On my PC I have created a system environment variable called 3DSMaxInstallDirectory

At the command line, if I give

echo %3DSMaxInstallDirectory%Plugins\

I get

D:\Program Files\Autodesk\3ds Max 2011\Plugins\

In Visual Studio I enter into the Post-Build section

copy "$(TargetDir)$(TargetName).*" "$(3DSMaxInstallDirectory)Plugins\"

However on build I get

Error   4   The command "copy "C:\Users\Sebastian\Documents\Visual Studio 2010\Projects\MaxBridge\MaxBridgeImporterPlugin\bin\Debug\MaxBridgePlugin.*" "Plugins\"
" exited with code 1.   MaxBridgeImporterPlugin

The results on Google are a confusing mix of suggestions that Visual Studio doesn't support EVs, Visual Studio does support EVs, Visual Studio needs %..% and Visual Studio needs $(..) - and none of which seem to work on my computer.

What is the correct way to use my environment variable in Visual Studio?

(Yes, the directory exists, and the reason I don't want to set the path explicitly is I am preparing to share this project, and every step someone else has to take after downloading and before building is another barrier.)

sebf
  • 2,831
  • 5
  • 32
  • 50

5 Answers5

6

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

rene
  • 41,474
  • 78
  • 114
  • 152
  • 1
    Thank you based on that I have found that '3DSMaxInstallDirectory' does not work, but '_3DSMaxInstallDirectory' does. I believe its due to Visual Studio escaping '%' to '%25' in the csproj, which when parsed becomes "%253" & "DSMax...". – sebf Aug 04 '13 at 12:48
  • 1
    This is weird! For me, using `echo %My Environmental Variable%\Some Sub Directory` resulted in `\Some Sub Directory`; using `%25`, as others have suggested resulted in `5My Environmental Variable's Value5\Some Sub Directory`, using `%2` actually gave the desired result with `echo` but then didn't with `xcopy` or `copy`! Everything I've tried with `xcopy` isn't substituting the Environmental Variable Name with its actual value. – Matt Arnold Jul 09 '20 at 16:28
  • @MattArnold what msbuild version is this and you are on Windows 10 or a comparable server OS, right? – rene Jul 09 '20 at 16:49
  • @rene I'm on Windows 10, building with Visual Studio 2019, the project is in .NET 4.0 - how would I find out the MSBuild Version? – Matt Arnold Jul 10 '20 at 09:14
  • @MattArnold in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\ open a command prompt and run `msbuild -ver`. I expect it to say 16.0 – rene Jul 10 '20 at 09:36
  • @rene I actually had to run this command (I'm using Professional, not Community): `C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin>MSBuild.exe -ver` but you were close, the version is `16.6.0.22303`. – Matt Arnold Jul 10 '20 at 12:45
5

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.

klaus triendl
  • 1,237
  • 14
  • 25
2

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
whitey04
  • 1,321
  • 11
  • 17
  • I believe the encoding that is missing is when you enter the custom command through the GUI (of course you must encode yourself if your editing the .vcxproj file). If you want to down vote; leave a comment. I had this problem and this is how I fixed it. Edit: Clarify results with/without encoding. – whitey04 Aug 25 '14 at 18:33
0

According to this MS Link, the syntax is now the same as using a project file variable, which means you don't have to worry about escaping the % character anymore. For example, if BIN_PATH is an environment variable and the following line is added to the post build step:

echo $(BIN_PATH)

This line will then display the value of BIN_PATH to the output window after the build.

But, be aware that the environment variable must be defined before VS is started. I lost about 45 minutes when I first tried this before it dawned on me that maybe the reason why it was not working was because VS loads the environment variables on startup. So, I exited VS, restarted, and it worked like a charm.

You still might need the old % character syntax if your environment variable is the same as a VS project variable, since "the property in the project file overrides the value of the environment variable."

Bob Bryan
  • 3,687
  • 1
  • 32
  • 45
0

If you want to use an environment variable that is defined after VS is started, the rules differ for .vcxproj and .csproj projects. In .vcxproj projects, writing %MY_ENV_VAR% in the Project Properties won't work - you need to use the escape syntax %25MY_ENV_VAR%25. But a .csproj project does accept %MY_ENV_VAR%; using the escape syntax causes the escape sequence to be mangled, giving %2525MY_ENV_VAR%2525. (This in VS2017 at least.)

David Pritchard
  • 101
  • 1
  • 2