views:

533

answers:

5

I am developing an auto-builder that will run a series of steps in our build process and build our target application. We used to use a batch file which set up a bunch of environment variables or called tools that setup environment variables and ultimately runs a 'make'.

I've been using the 'Process' class which works great for running those commands but unfortunately every time one runs which makes changes to the environment (like adding something to the PATH) those variables are lost when the 'Process' completes. The next 'Process' is instantiated and inherits the env from the 'calling' app (my exe) again - which means all env setup by the last command are lost. How do you handle this situation? Is there a better way to run a series of batch-file like commands within C# and maintain the environment they set up?

Please note that unfortunately the old-schoolers have declared that nant/ant are not an option so "Hey, why not use Nant - it does that!" is not the answer I am looking for.

Thanks.

A: 

Well, the System.Environment.SetEnvironmentVariable() method will let you specify the scope for a variable that you set. Is this what you are looking for? Not sure that I understand.

EBGreen
A: 

We use CruiseControl.net to run an NAnt script. Highly recommended.

The NAnt script can be invoked using -D: command-line switch to set the equivalent of environment variables.

Craig Eddy
Is there a way to do this with MSBuild instead? I think the anti-NAnt camp wouldn't mind using that, since it comes from MS.
Jon Limjap
A: 

i would suggest some code that would save your environment variables to an external file, and then you can retrieve these variables via the external file at the start of following processes.

John West
A: 

I think the problem is not in specifying custom environment variables here. (You can set them via ProcessStartInfo.) The problem is in reading the changes made to environment variables by the processes being run. I'm not sure if it's possible. The only ways I know set environment variables for the process itself and/or for its child processes. I don't know no way to set environment variables for a parent process.

Orlangur
Yeah - this would be a good solution if there were a way to get the modified variables from the previously run Process but I don't see any way to do that.
Eyesno
A: 

Environment variables are never set and cannot be set for the parent process (*). Only for the current process and the ones it creates - that is part of the concept.

(*) apart from, maybe, messing around with OS-internals.

Christian.K