Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010: How to use Environment Variables in Post-Build

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.)

like image 771
sebf Avatar asked Aug 04 '13 11:08

sebf


People also ask

How do you use environment variables?

Environment variables are used according to a specific precedence order, as follows: Environment variables declared inside a shell command in a run step, for example FOO=bar make install . Environment variables declared with the environment key for a run step.

Where do I put environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do I set environment variables in Visual Studio?

In Visual Studio 2019 right-click your project, choose Properties . In the project properties window, select the Debug tab. Then, under Environment variables change the value of your environment from Development to Production or other environments.

How do I set an environment variable in export?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").


1 Answers

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.

like image 156
klaus triendl Avatar answered Sep 19 '22 05:09

klaus triendl