如何在C#中运行一系列进程并保留其环境设置?

时间:2021-07-21 01:10:25

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'.

我正在开发一个自动构建器,它将在构建过程中运行一系列步骤并构建我们的目标应用程序。我们曾经使用批处理文件来设置一堆环境变量或称为工具来设置环境变量并最终运行'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?

我一直在使用'Process'类,它非常适合运行这些命令但不幸的是每次运行时都会对环境进行更改(比如向PATH添加内容),当'Process'完成时,这些变量就会丢失。下一个'Process'被实例化并再次从'calling'应用程序(我的exe)继承env - 这意味着最后一个命令设置的所有env都会丢失。你是如何处理这种情况的?有没有更好的方法在C#中运行一系列类似批处理文件的命令并维护它们设置的环境?

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.

请注意,不幸的是,老同学们宣称nant / ant不是一个选项所以“嘿,为什么不使用Nant-它就是这样!”不是我要找的答案。

Thanks.

5 个解决方案

#1


0  

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.

我建议一些代码将环境变量保存到外部文件,然后您可以在以下进程开始时通过外部文件检索这些变量。

#2


0  

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.

好吧,System.Environment.SetEnvironmentVariable()方法将允许您指定您设置的变量的范围。这是你想要的?不确定我理解。

#3


0  

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

我们使用CruiseControl.net来运行NAnt脚本。强烈推荐。

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

可以使用-D:命令行开关调用NAnt脚本来设置环境变量的等效项。

#4


0  

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.

我认为问题不在于在此处指定自定义环境变量。 (您可以通过ProcessStartInfo设置它们。)问题在于读取正在运行的进程对环境变量所做的更改。我不确定是否可能。我知道的唯一方法是为流程本身和/或子流程设置环境变量。我不知道没有办法为父进程设置环境变量。

#5


0  

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.

(*)除了,也许,搞乱OS-internals。

#1


0  

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.

我建议一些代码将环境变量保存到外部文件,然后您可以在以下进程开始时通过外部文件检索这些变量。

#2


0  

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.

好吧,System.Environment.SetEnvironmentVariable()方法将允许您指定您设置的变量的范围。这是你想要的?不确定我理解。

#3


0  

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

我们使用CruiseControl.net来运行NAnt脚本。强烈推荐。

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

可以使用-D:命令行开关调用NAnt脚本来设置环境变量的等效项。

#4


0  

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.

我认为问题不在于在此处指定自定义环境变量。 (您可以通过ProcessStartInfo设置它们。)问题在于读取正在运行的进程对环境变量所做的更改。我不确定是否可能。我知道的唯一方法是为流程本身和/或子流程设置环境变量。我不知道没有办法为父进程设置环境变量。

#5


0  

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.

(*)除了,也许,搞乱OS-internals。