Updating the Path environment variable from nAnt scripts
I couldn't seem to find a way to update the Path environment variable using the 'stable' build of nAnt. Setting sys.env.Path doesn't update the system environment variable, though the ${sys.env.Path} property will appear to be updated. This can be somewhat confusing.
So, I've written a target, instead. This "updatepath" target will update the path for the length of the nAnt build. Set the "build.appendpath" property before calling this target, and be certain to call the <sysinfo/> task as well.
A similar method could be used for any environment variable:
<target name="updatepath">
<script language="C#">
<code><![CDATA[
public static void ScriptMain(Project project)
{
string currentPath = project.Properties["sys.env.Path"];
// make certain that there is a trailing semicolon
if(!(currentPath.EndsWith(";"))) currentPath += ";";
string updatedPath = currentPath + project.Properties["build.appendpath"];
SetEnvironmentVariable("Path", updatedPath);
} [ System.Runtime.InteropServices.DllImport( "kernel32") ]
public static extern bool SetEnvironmentVariable( string EnvironmentVariable, string EnvironmentValue ); ]]></code>
</script>
</target>