MSBuild: By Example—Using Environment Variables in Your Project
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| Visual Studio Articles |
|
| © 2006 Hashimi & Hashimi |
Using Environment Variables in Your Project
In Chapter 2, you were exposed to what properties are and how to use them. Properties are similar to something you may already be familiar with—environment variables. The main difference is that a property is defined within the scope of an MSBuild execution, whereas an environment variable is available throughout the machine. In your builds, you may want to reference these environment variables, and doing so is easy! To use the value of an environment variable, you simply refer to that just as you would if it were a property. Refer to the following target, which prints the value for the Path environment variable:
<Target Name="PrintSystemPath"> <Message Text="Path: $(Path)"/> </Target>
To invoke this target at the command line, execute >msbuild MetaDataEx.csproj /t:PrintSystemPath. Figure 3-15 shows the output from this invocation.
![]()
Figure 3-15. Output from the PrintSystemPath target
In Figure 3-15 you can see the system path’s value output to the console. As you can see, using a property is just as easy as using a property that is defined in your MSBuild project file. All environment variables are available to be used just as properties are in your MSBuild project files.
|

