您可以使用Exec
任务来破坏它,或者您可以编写自己的Task
来设置它们 - 这是“让我们用 Exec 破坏”路线:
<PropertyGroup>
<!--
need the CData since this blob is just going to
be embedded in a mini batch file by studio/msbuild
-->
<LibSetter><![CDATA[
set Lib=C:\Foo\Bar\Baz
set AnyOtherEnvVariable=Hello!
]]></LibSetter>
</PropertyGroup>
<Exec Command="$(LibSetter)" />
编辑:所以我只是把这个 csproj 和基础知识一起扔了——我已经确认它们在我运行它们时设置正确——我也添加了 inline-Task 方法。
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="EnvVarSet"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<VarName ParameterType="System.String" Required="true"/>
<VarValue ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
Console.WriteLine("Setting var name {0} to {1}...", VarName, VarValue);
System.Environment.SetEnvironmentVariable(VarName, VarValue);
Console.WriteLine("{0}={1}", VarName, VarValue);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="ThingThatNeedsEnvironmentVars">
<CallTarget Targets="FiddleWithEnvironmentVars"/>
<Message Text="LIB environment var is now: $([System.Environment]::GetEnvironmentVariable('LIB'))"/>
</Target>
<Target Name="FiddleWithEnvironmentVars">
<Message Text="LIB environment var is now: $([System.Environment]::GetEnvironmentVariable('LIB'))"/>
<EnvVarSet VarName="LIB" VarValue="C:\temp"/>
<Message Text="LIB environment var is now: $([System.Environment]::GetEnvironmentVariable('LIB'))"/>
</Target>
</Project>