How to 'set' an environment variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Davidson

    How to 'set' an environment variable?

    All:

    I've found the 'Environment.Ge tEnvironmentVar iable()' method; but how do I
    create and/or set an environment variable?

    Thanks,
    Bill


  • Felix Wang

    #2
    RE: How to 'set' an environment variable?

    Hi Bill,

    Thanks for posting.

    System environment variables are stored in the following key:

    HKEY_LOCAL_MACH INE\SYSTEM\Cont rolSet001\Contr ol\Session Manager\Environ ment

    User variables are stored in the following key:

    HKEY_CURRENT_US ER\Environment

    Maybe we can use the Microsoft.Win32 .Registry and the RegistryKey classes
    to manipulate them directly.

    I hope this makes sense to you.

    Regards,

    Felix Wang
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.

    Comment

    • Bill Davidson

      #3
      Re: How to 'set' an environment variable?

      Thanks,

      Perhaps I'll just call the SetEnvironmentV ariable() Win32 API via P/Invoke.
      I just figured the Framework Class Libraries wrapped this call, but I just
      couldn't find it.

      Thanks for your help,
      Bill


      "Felix Wang" <v-felwa@online.mi crosoft.com> wrote in message
      news:UV12a1osDH A.1248@cpmsftng xa07.phx.gbl...[color=blue]
      > Hi Bill,
      >
      > Thanks for posting.
      >
      > System environment variables are stored in the following key:
      >
      > HKEY_LOCAL_MACH INE\SYSTEM\Cont rolSet001\Contr ol\Session[/color]
      Manager\Environ ment[color=blue]
      >
      > User variables are stored in the following key:
      >
      > HKEY_CURRENT_US ER\Environment
      >
      > Maybe we can use the Microsoft.Win32 .Registry and the RegistryKey classes
      > to manipulate them directly.
      >
      > I hope this makes sense to you.
      >
      > Regards,
      >
      > Felix Wang
      > Microsoft Online Partner Support
      > Get Secure! - www.microsoft.com/security
      > This posting is provided "as is" with no warranties and confers no rights.
      >[/color]


      Comment

      • Eric Newton

        #4
        Re: How to 'set' an environment variable?

        The reason why you're probably never gonna see SetEnvironmentV ariable in te
        framework is due to the encouragement of utilizing config files as your
        settings. Of course, we as developers can fight that by adding things back
        in, but the focus has shifted from Environement Variables -> Registry ->
        config files, and I think its a good change... its a whole lot easier to
        shift config files around then it is to write a batch file or a "Reg Import"
        when you want to duplicate or just move apps around.

        --
        Eric Newton
        C#/ASP Application Developer

        eric@cc.ensoft-software.com [remove the first "CC."]

        "Bill Davidson" <bill.davidson@ getronics.com> wrote in message
        news:uZgolnpsDH A.2208@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Thanks,
        >
        > Perhaps I'll just call the SetEnvironmentV ariable() Win32 API via[/color]
        P/Invoke.[color=blue]
        > I just figured the Framework Class Libraries wrapped this call, but I just
        > couldn't find it.
        >
        > Thanks for your help,
        > Bill
        >
        >
        > "Felix Wang" <v-felwa@online.mi crosoft.com> wrote in message
        > news:UV12a1osDH A.1248@cpmsftng xa07.phx.gbl...[color=green]
        > > Hi Bill,
        > >
        > > Thanks for posting.
        > >
        > > System environment variables are stored in the following key:
        > >
        > > HKEY_LOCAL_MACH INE\SYSTEM\Cont rolSet001\Contr ol\Session[/color]
        > Manager\Environ ment[color=green]
        > >
        > > User variables are stored in the following key:
        > >
        > > HKEY_CURRENT_US ER\Environment
        > >
        > > Maybe we can use the Microsoft.Win32 .Registry and the RegistryKey[/color][/color]
        classes[color=blue][color=green]
        > > to manipulate them directly.
        > >
        > > I hope this makes sense to you.
        > >
        > > Regards,
        > >
        > > Felix Wang
        > > Microsoft Online Partner Support
        > > Get Secure! - www.microsoft.com/security
        > > This posting is provided "as is" with no warranties and confers no[/color][/color]
        rights.[color=blue][color=green]
        > >[/color]
        >
        >[/color]


        Comment

        • Bill Davidson

          #5
          Re: How to 'set' an environment variable?

          Eric:
          [color=blue]
          > The reason why you're probably never gonna see SetEnvironmentV ariable in[/color]
          te[color=blue]
          > framework is due to the encouragement of utilizing config files as your
          > settings[/color]

          I figured this might be the case, but the Framework provides the
          EnvironmentPerm ission class with the EnvironmentPerm issionAccess enumeration
          that contains the 'Write' Permission defined as follows: "Write access to
          environment variables is specified. Write access includes creating and
          deleting environment variables as well as changing existing values. Reading
          environment variables is not included in this access level".

          'Seems like the .Net security model is all set up for handling environment
          variables, but the methods for actually writing/creating these variables are
          nowhere to be found.

          Anyway, I found a workaround so I'll close this thread. Thanks to everyone
          for their assistance.

          Bill



          "Eric Newton" <eric@cc.enso ft-software.com> wrote in message
          news:#eskk5rsDH A.2380@TK2MSFTN GP09.phx.gbl...[color=blue]
          > The reason why you're probably never gonna see SetEnvironmentV ariable in[/color]
          te[color=blue]
          > framework is due to the encouragement of utilizing config files as your
          > settings. Of course, we as developers can fight that by adding things[/color]
          back[color=blue]
          > in, but the focus has shifted from Environement Variables -> Registry ->
          > config files, and I think its a good change... its a whole lot easier to
          > shift config files around then it is to write a batch file or a "Reg[/color]
          Import"[color=blue]
          > when you want to duplicate or just move apps around.
          >
          > --
          > Eric Newton
          > C#/ASP Application Developer
          > http://ensoft-software.com/
          > eric@cc.ensoft-software.com [remove the first "CC."]
          >
          > "Bill Davidson" <bill.davidson@ getronics.com> wrote in message
          > news:uZgolnpsDH A.2208@TK2MSFTN GP10.phx.gbl...[color=green]
          > > Thanks,
          > >
          > > Perhaps I'll just call the SetEnvironmentV ariable() Win32 API via[/color]
          > P/Invoke.[color=green]
          > > I just figured the Framework Class Libraries wrapped this call, but I[/color][/color]
          just[color=blue][color=green]
          > > couldn't find it.
          > >
          > > Thanks for your help,
          > > Bill
          > >
          > >
          > > "Felix Wang" <v-felwa@online.mi crosoft.com> wrote in message
          > > news:UV12a1osDH A.1248@cpmsftng xa07.phx.gbl...[color=darkred]
          > > > Hi Bill,
          > > >
          > > > Thanks for posting.
          > > >
          > > > System environment variables are stored in the following key:
          > > >
          > > > HKEY_LOCAL_MACH INE\SYSTEM\Cont rolSet001\Contr ol\Session[/color]
          > > Manager\Environ ment[color=darkred]
          > > >
          > > > User variables are stored in the following key:
          > > >
          > > > HKEY_CURRENT_US ER\Environment
          > > >
          > > > Maybe we can use the Microsoft.Win32 .Registry and the RegistryKey[/color][/color]
          > classes[color=green][color=darkred]
          > > > to manipulate them directly.
          > > >
          > > > I hope this makes sense to you.
          > > >
          > > > Regards,
          > > >
          > > > Felix Wang
          > > > Microsoft Online Partner Support
          > > > Get Secure! - www.microsoft.com/security
          > > > This posting is provided "as is" with no warranties and confers no[/color][/color]
          > rights.[color=green][color=darkred]
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...