Take the 2-minute tour ×

I am trying to set environment variables using project file (ex. .vcxproj)

I looked at property functions but it didn't seem to have such function.

I know there is a way to retrieve environment variable but couldn't find how to set it.

I feel like there should be way to set environment variables in project file.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

A couple of things:

1) If you are only using the variable in the context of MSBuild, then you can just use the standard MSBuild variables instead of trying to set an environment variable

2) If do need to set an env var, well, it's not an out-of-box thing. You need to write a custom task and then leverage it in the project file. Here's a link to an MSDN thread that outlines how to do this.

share|improve this answer
Thank you for the answer! –  in His Steps Jan 11 '13 at 17:36
add comment

The coded task can be put right at the project file since MSBuild v4.0. Like this:


    <Name ParameterType="System.String" Required="true" />
    <Value ParameterType="System.String" Required="true" />

    <Using Namespace="System" />
    <Code Type="Fragment" Language="cs">
        Environment.SetEnvironmentVariable(Name, Value);


And then used from the target:

<Target Name="SampleTarget" BeforeTargets="Build">
  <SetEnvironmentVariableTask Name="TEST_ENV_VAR" Value="$(MSBuildProjectName)" />

That's much handier than authoring a separate .DLL for small MSBuild task(s).

share|improve this answer
Try as I might, I could not get this to work with Visual Studio 2012. However, I was able to find a solution here. –  Matt Davis Dec 19 '13 at 18:10
add comment

Your Answer


