Setting multiple environment variables for a Process object
-
In a C# application I create a
Process
object and asProcess.StartInfo.Arguments
I set an environment variable like this:Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/k SET MYVAR1=HELLO";
p.Start():But know I need to add another environment variable also valid for the cmd.exe process. The process object needs to pass a second variable called
MYVAR2=WORLD
. But theStartInfo.Arguments
is just astring
and if I try something like this:...
p.StartInfo.Arguments = @"/k SET MYVAR1=HELLO MYVAR2=WORLD";
...it doesn't work since
MYVAR1
then gets the value:HELLO MYVAR2=WORLD
. Does anyone have a clue on how to achieve this? -
In a C# application I create a
Process
object and asProcess.StartInfo.Arguments
I set an environment variable like this:Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/k SET MYVAR1=HELLO";
p.Start():But know I need to add another environment variable also valid for the cmd.exe process. The process object needs to pass a second variable called
MYVAR2=WORLD
. But theStartInfo.Arguments
is just astring
and if I try something like this:...
p.StartInfo.Arguments = @"/k SET MYVAR1=HELLO MYVAR2=WORLD";
...it doesn't work since
MYVAR1
then gets the value:HELLO MYVAR2=WORLD
. Does anyone have a clue on how to achieve this?Try seting the Arguments like this:
/K SET MYVAR1=HELLO && SET MYVAR2=WORLD
[EDIT] Added SET to the command after &&.
A guide to posting questions on CodeProject[^]
Dave Kreskowiakmodified on Wednesday, December 29, 2010 12:59 PM
-
In a C# application I create a
Process
object and asProcess.StartInfo.Arguments
I set an environment variable like this:Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/k SET MYVAR1=HELLO";
p.Start():But know I need to add another environment variable also valid for the cmd.exe process. The process object needs to pass a second variable called
MYVAR2=WORLD
. But theStartInfo.Arguments
is just astring
and if I try something like this:...
p.StartInfo.Arguments = @"/k SET MYVAR1=HELLO MYVAR2=WORLD";
...it doesn't work since
MYVAR1
then gets the value:HELLO MYVAR2=WORLD
. Does anyone have a clue on how to achieve this?First step is to realize that the shell, not Process is processing the env var command which you are passing. So Process itself, as you are using it, is meaningless in the context of the question. It would require using so feature of 'cmd' itself. The other response referring to '&&' could be made to work although I doubt the exact form suggested is correct. However ProcessStartInfo (which is the type of Process.StartInfo) has a property called EnvironmentVariables. That is probably what you want.
-
Try seting the Arguments like this:
/K SET MYVAR1=HELLO && SET MYVAR2=WORLD
[EDIT] Added SET to the command after &&.
A guide to posting questions on CodeProject[^]
Dave Kreskowiakmodified on Wednesday, December 29, 2010 12:59 PM
-
First step is to realize that the shell, not Process is processing the env var command which you are passing. So Process itself, as you are using it, is meaningless in the context of the question. It would require using so feature of 'cmd' itself. The other response referring to '&&' could be made to work although I doubt the exact form suggested is correct. However ProcessStartInfo (which is the type of Process.StartInfo) has a property called EnvironmentVariables. That is probably what you want.
Yours is the better solution. I just kept my answer within the bounds of the code he already has. The "&&" does work though. You can try it yourself in a simple Start -> Run line:
CMD /K SET MYVAR1=HELLO && SET MYVAR2=WORLD
When the CMD box shows up, just type SET and look for the variables.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
First step is to realize that the shell, not Process is processing the env var command which you are passing. So Process itself, as you are using it, is meaningless in the context of the question. It would require using so feature of 'cmd' itself. The other response referring to '&&' could be made to work although I doubt the exact form suggested is correct. However ProcessStartInfo (which is the type of Process.StartInfo) has a property called EnvironmentVariables. That is probably what you want.
Thank you for your reply. I know that environment variables are used by shell and not
Process
per se, but as you saw in the example I need to start a cmd process and set more than one variable. I don't think using theEnvironmentVariables
property ofProcessStartInfo
will work since it seems to be readonly according to MSDN. Anyway, it works with the previous suggestion (with a slight correction) as shown below./K SET MYVAR1=HELLO && SET MYVAR2=WORLD
-
Yours is the better solution. I just kept my answer within the bounds of the code he already has. The "&&" does work though. You can try it yourself in a simple Start -> Run line:
CMD /K SET MYVAR1=HELLO && SET MYVAR2=WORLD
When the CMD box shows up, just type SET and look for the variables.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Thank you for your reply. I know that environment variables are used by shell and not
Process
per se, but as you saw in the example I need to start a cmd process and set more than one variable. I don't think using theEnvironmentVariables
property ofProcessStartInfo
will work since it seems to be readonly according to MSDN. Anyway, it works with the previous suggestion (with a slight correction) as shown below./K SET MYVAR1=HELLO && SET MYVAR2=WORLD
-
Dave Kreskowiak wrote:
The "&&" does work though. You can try it yourself in a simple Start -> Run line:
That would of course be a variation - as a I suggested. Your first reply is missing the second 'set'.
Oh *#&$, I did screw that up, didn't I... Thanks for the extra pair of eyes!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Calla wrote:
EnvironmentVariables property of ProcessStartInfo will work since it seems to be readonly according to MSDN.
The collection itself is readonly. The items in the collection are not.