.NET Framework Bookmark and Share   
 index > MSBuild > How to set envrionment variables in MSBuild file?
 

How to set envrionment variables in MSBuild file?

Experts,
   In a msbuild file, How do I set a environment variable?

Thanks,
Hercules

I wrote a task to do this.  It is pretty simple:



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace MSBuildTasks
{
    public class SetEnvVar : Task
    {
        private string _variable;
        private string _value;

        [Required]
        public string Variable
        {
            get { return _variable; }
            set { _variable = value; }
        }

        [Required]
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
 
        public override bool Execute()
        {
            Environment.SetEnvironmentVariable(_variable, _value);
            return true;
        }
    }
}

 



Use it like so:



 <UsingTask TaskName="MSBuildTasks.SetEnvVar"
         AssemblyFile="$(RootDir)Tools\Bin\MSBuildTasks.dll"/>

 <PropertyGroup>
  <DevEnvDir>$(VS80COMNTOOLS)..\IDE</DevEnvDir>
  <VsInstallDir>$(VS80COMNTOOLS)..\..</VsInstallDir>
  <VcInstallDir>$(VS80COMNTOOLS)..\..\VC</VcInstallDir>
  <FrameworkDir>$(windir)\Microsoft.NET\Framework</FrameworkDir>
  <FrameworkVersion>v2.0.50712</FrameworkVersion>
  <FrameworkSDKDir>$(VsInstallDir)\SDK\v2.0</FrameworkSDKDir>
  <PATH>$(DevEnvDir);$(VcInstallDir)\BIN;$(VS80COMNTOOLS);$(VS80COMNTOOLS)Bin;$(VcInstallDir)\PlatformSDK\bin;$(FrameworkSDKDir)\bin;$(FrameworkDir)\$(FrameworkVersion);$(VcInstallDir)\VCPackages;$(PATH)</PATH>
  <INCLUDE>$(VcInstallDir)\ATLMFC\INCLUDE;$(VcInstallDir)\INCLUDE;$(VcInstallDir)\PlatformSDK\include;$(FrameworkSDKDir)\include;$(INCLUDE)</INCLUDE>
  <LIB>$(VcInstallDir)\ATLMFC\LIB;$(VcInstallDir)\LIB;$(VcInstallDir)\PlatformSDK\lib;$(FrameworkSDKDir)\lib;$(LIB)</LIB>
  <LIBPATH>$(FrameworkDir)\$(FrameworkVersion)</LIBPATH>
 </PropertyGroup>

 <Target Name="PrepareBuildEnvironment">
  <!-- To dump the env run msbuild with /p:DumpEnv=y switch -->
  <SetEnvVar Variable="DevEnvDir" Value="$(DevEnvDir)"/>
  <SetEnvVar Variable="VsInstallDir" Value="$(VsInstallDir)"/>
  <SetEnvVar Variable="VcInstallDir" Value="$(VcInstallDir)"/>
  <SetEnvVar Variable="FrameworkDir" Value="$(FrameworkDir)"/>
  <SetEnvVar Variable="FrameworkVersion" Value="$(FrameworkVersion)"/>
  <SetEnvVar Variable="FrameworkSDKDir" Value="$(FrameworkSDKDir)"/>
  <SetEnvVar Variable="PATH" Value="$(PATH)"/>
  <SetEnvVar Variable="INCLUDE" Value="$(INCLUDE)"/>
  <SetEnvVar Variable="LIB" Value="$(LIB)"/>
  <SetEnvVar Variable="LIBPATH" Value="$(LIBPATH)"/>
  <Warning Condition="!Exists('$(FrameworkDir)\$(FrameworkVersion)')" Text="Framework dir '$(FrameworkDir)\$(FrameworkVersion)' doesn't exist. Check version number."/>
  <Message Condition="'$(DumpEnv)' != ''" Text="DumpEnv Requested:" />
  <Exec Condition="'$(DumpEnv)' != ''" Command="set" />
 </Target>

 


Keith Hill

You'd have to write a task - there's no way to set environment variables by default.

But what is your scenario?  What are you trying to do that you wish to use environment variables for?

jeff
tester on msbuild

Jeffery Callahan

I wrote a task to do this.  It is pretty simple:



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace MSBuildTasks
{
    public class SetEnvVar : Task
    {
        private string _variable;
        private string _value;

        [Required]
        public string Variable
        {
            get { return _variable; }
            set { _variable = value; }
        }

        [Required]
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
 
        public override bool Execute()
        {
            Environment.SetEnvironmentVariable(_variable, _value);
            return true;
        }
    }
}

 



Use it like so:



 <UsingTask TaskName="MSBuildTasks.SetEnvVar"
         AssemblyFile="$(RootDir)Tools\Bin\MSBuildTasks.dll"/>

 <PropertyGroup>
  <DevEnvDir>$(VS80COMNTOOLS)..\IDE</DevEnvDir>
  <VsInstallDir>$(VS80COMNTOOLS)..\..</VsInstallDir>
  <VcInstallDir>$(VS80COMNTOOLS)..\..\VC</VcInstallDir>
  <FrameworkDir>$(windir)\Microsoft.NET\Framework</FrameworkDir>
  <FrameworkVersion>v2.0.50712</FrameworkVersion>
  <FrameworkSDKDir>$(VsInstallDir)\SDK\v2.0</FrameworkSDKDir>
  <PATH>$(DevEnvDir);$(VcInstallDir)\BIN;$(VS80COMNTOOLS);$(VS80COMNTOOLS)Bin;$(VcInstallDir)\PlatformSDK\bin;$(FrameworkSDKDir)\bin;$(FrameworkDir)\$(FrameworkVersion);$(VcInstallDir)\VCPackages;$(PATH)</PATH>
  <INCLUDE>$(VcInstallDir)\ATLMFC\INCLUDE;$(VcInstallDir)\INCLUDE;$(VcInstallDir)\PlatformSDK\include;$(FrameworkSDKDir)\include;$(INCLUDE)</INCLUDE>
  <LIB>$(VcInstallDir)\ATLMFC\LIB;$(VcInstallDir)\LIB;$(VcInstallDir)\PlatformSDK\lib;$(FrameworkSDKDir)\lib;$(LIB)</LIB>
  <LIBPATH>$(FrameworkDir)\$(FrameworkVersion)</LIBPATH>
 </PropertyGroup>

 <Target Name="PrepareBuildEnvironment">
  <!-- To dump the env run msbuild with /p:DumpEnv=y switch -->
  <SetEnvVar Variable="DevEnvDir" Value="$(DevEnvDir)"/>
  <SetEnvVar Variable="VsInstallDir" Value="$(VsInstallDir)"/>
  <SetEnvVar Variable="VcInstallDir" Value="$(VcInstallDir)"/>
  <SetEnvVar Variable="FrameworkDir" Value="$(FrameworkDir)"/>
  <SetEnvVar Variable="FrameworkVersion" Value="$(FrameworkVersion)"/>
  <SetEnvVar Variable="FrameworkSDKDir" Value="$(FrameworkSDKDir)"/>
  <SetEnvVar Variable="PATH" Value="$(PATH)"/>
  <SetEnvVar Variable="INCLUDE" Value="$(INCLUDE)"/>
  <SetEnvVar Variable="LIB" Value="$(LIB)"/>
  <SetEnvVar Variable="LIBPATH" Value="$(LIBPATH)"/>
  <Warning Condition="!Exists('$(FrameworkDir)\$(FrameworkVersion)')" Text="Framework dir '$(FrameworkDir)\$(FrameworkVersion)' doesn't exist. Check version number."/>
  <Message Condition="'$(DumpEnv)' != ''" Text="DumpEnv Requested:" />
  <Exec Condition="'$(DumpEnv)' != ''" Command="set" />
 </Target>

 


Keith Hill
sorry, this doesnt work on my end.

<PropertyGroup>
<DevEnvDir>$(VS80COMNTOOLS)..\IDE</DevEnvDir>
</PropertyGroup>


How can i get the current DevEnvDir? Thanks.
Rodel E. Dagumampan
Hmm... that should work. Here's example script -

<Project DefaultTargets="T"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<DevEnvDir>$(VS80COMNTOOLS)..\IDE</DevEnvDir>
</PropertyGroup>

<Target Name="T">
<Message Text=" $(VS80COMNTOOLS) "/>
<Message Text=" $(DevEnvDir) "/>
</Target>


</Project>

Here's the output

Target T:
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\..\IDE

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:00.04
Raghu Dodda
Hi Jeffery - I know this is an Ancient Post, but you asked for a usage scenario and I can give you one. Sandcastle (www.codeplex.com/Sandcastle) would be much easier to work with if we could set the DXROOT environment variable and the PATH before we use it to build. Something like this would be very helpful:

<Exec CommandLine="program.exe">
<EnvironmentVariables>
<DXROOT>c:\Program Files\Sandcastle</DXROOT>
<Path>c:\Program Files\Sandcastle\bin;%path%</Path>
</EnvironmentVariables>
</Exec>

Note....Nant does it like this.

Best regards, Doug
Doug Clutter

You can use google to search for other answers

Custom Search

More Threads

• How to override $(NoWarn) ?
• Select specific item in item group
• Code generation task
• Output param from a custom Task
• J# 2.0 bootstrapper
• strange file not found error..
• New Microsoft SDC Tasks - Release 2.1.3041.0 (29 April 08)
• MSBuild best practice
• Configuration='Debug' Platform='BNB'
• Copying files