Monday, November 08, 2004 3:57 PM jerdenn

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>

Filed under:

Comments

# re: Updating the Path environment variable from nAnt scripts

Monday, December 06, 2004 9:41 AM by PJB

Is there no other way of achieving this, although your solution is nice, it seems odd that NAnt cannot cope with modifying environments??

# re: Updating the Path environment variable from nAnt scripts

Monday, December 06, 2004 2:26 PM by Jerry Dennany

If I could have found an easier way to do this in nAnt, I would have used it. If there is an easier way in 0.84, I'd like to know, myself.

# re: Updating the Path environment variable from nAnt scripts

Wednesday, December 08, 2004 5:38 PM by Eric Deslauriers

So, for some reason, that's not working for me on 0.84.

Here's a simplified version that should be pretty straightforward:

<target name="updatepath">
<property name="build.appendpath" value="test"/>
<script language="C#">
<code><![CDATA[

public static void ScriptMain(Project project)
{
string x = project.Properties["sys.env.x"];
System.Console.WriteLine("x was: " + x);
System.Console.WriteLine("Changing to: " + project.Properties["build.appendpath"]);
SetEnvironmentVariable("x", project.Properties["build.appendpath"]);
System.Console.WriteLine("x is: " + x);
}


[ System.Runtime.InteropServices.DllImport( "kernel32") ]
public static extern bool SetEnvironmentVariable( string EnvironmentVariable, string EnvironmentValue );


]]></code>
</script>
<echo message="x=${sys.env.x}"/>
</target>

When you run this, the output is:
updatepath:

x was: 0
Changing to: test
x is: 0
[echo] x=0

BUILD SUCCEEDED

What'd I do wrong?

# Setting Environment Variables with NAnt

Tuesday, December 14, 2004 8:19 PM by TrackBack

question came up on NAnt-Users about setting env vars using NAnt. This has been asked a few times before. Unfortunately the .Net environment class does not support writing env var values...

# Updating the Path environment variable from nAnt scripts, Redux...

Sunday, January 16, 2005 1:55 AM by TrackBack

Leave a Comment

(required) 
(required) 
(optional)
(required)