Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Setting multiple environment variables for a Process object

Setting multiple environment variables for a Process object

Scheduled Pinned Locked Moved C#
csharptutorialquestionworkspace
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calla
    wrote on last edited by
    #1

    In a C# application I create a Process object and as Process.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 the StartInfo.Arguments is just a string 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?

    D J 2 Replies Last reply
    0
    • C Calla

      In a C# application I create a Process object and as Process.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 the StartInfo.Arguments is just a string 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?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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 Kreskowiak

      modified on Wednesday, December 29, 2010 12:59 PM

      C 1 Reply Last reply
      0
      • C Calla

        In a C# application I create a Process object and as Process.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 the StartInfo.Arguments is just a string 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?

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        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.

        D C 2 Replies Last reply
        0
        • D Dave Kreskowiak

          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 Kreskowiak

          modified on Wednesday, December 29, 2010 12:59 PM

          C Offline
          C Offline
          Calla
          wrote on last edited by
          #4

          Thanks Dave, I'll try that when I'm back at the office.

          1 Reply Last reply
          0
          • J jschell

            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.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • J jschell

              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.

              C Offline
              C Offline
              Calla
              wrote on last edited by
              #6

              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 the EnvironmentVariables property of ProcessStartInfo 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

              J 1 Reply Last reply
              0
              • D Dave Kreskowiak

                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

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

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

                D 1 Reply Last reply
                0
                • C Calla

                  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 the EnvironmentVariables property of ProcessStartInfo 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

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  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.

                  C 1 Reply Last reply
                  0
                  • J jschell

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

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • J jschell

                      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.

                      C Offline
                      C Offline
                      Calla
                      wrote on last edited by
                      #10

                      I see. Thank you for pointing that out!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups