Environment Variables Go "Poof"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott McNair

    Environment Variables Go "Poof"

    Hi,

    I'm trying to set an environment variable programmaticall y. I've added
    these three lines to my code:

    System.Environm ent.SetEnvironm entVariable("TC S", "C:\Program Files\TCS\")
    InstallPath = System.Environm ent.GetEnvironm entVariable("TC S")
    MsgBox(InstallP ath)

    And, as expected, the MsgBox contains "C:\Program Files\TCS\".

    However the variable seems to exist only for that instance of the app. For
    example, if I open a cmd window while the app is running and type "echo %
    TCS%", it just returns "%TCS%". If I exit the app, remark out the first
    line, and then re-run the app, the MsgBox returns an empty box.

    I'm guessing that any environment variables you set using
    System.Environm ent.SetEnvironm entVariable are volatile; that is, they don't
    exist outside that currently running application.

    How can I set it programmaticall y, and have it STAY there?

    Regards,
    Scott
  • Andrew Backer

    #2
    Re: Environment Variables Go "Poof&quot ;

    Take a look at this, which might help you. It may come down to updating
    the registry.

    Modifying environment variables seems like one of those easy tasks that you would expect .Net to support. Unfortunatley the System.Environme...

    Hi,
    >
    I'm trying to set an environment variable programmaticall y. I've
    added these three lines to my code:
    >
    System.Environm ent.SetEnvironm entVariable("TC S", "C:\Program
    Files\TCS\") InstallPath =
    System.Environm ent.GetEnvironm entVariable("TC S") MsgBox(InstallP ath)
    >
    And, as expected, the MsgBox contains "C:\Program Files\TCS\".
    >
    However the variable seems to exist only for that instance of the app.
    For example, if I open a cmd window while the app is running and type
    "echo % TCS%", it just returns "%TCS%". If I exit the app, remark out
    the first line, and then re-run the app, the MsgBox returns an empty
    box.
    >
    I'm guessing that any environment variables you set using
    System.Environm ent.SetEnvironm entVariable are volatile; that is, they
    don't exist outside that currently running application.
    >
    How can I set it programmaticall y, and have it STAY there?
    >
    Regards,
    Scott

    Comment

    • Scott McNair

      #3
      Re: Environment Variables Go "Poof&quot ;

      Tom Shelton <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in
      news:uXr2JI9fIH A.3940@TK2MSFTN GP05.phx.gbl:
      If you have any issues let me know and I'll actually look up
      SendMessage and test some code :)
      Thanks, Tom.

      I entered your code, and it seems to hang when I use SendMessage.

      Comment

      • Tom Shelton

        #4
        Re: Environment Variables Go &quot;Poof&quot ;

        On 2008-03-06, Scott McNair <smcnair@beache xpress.takethis partout.comwrot e:
        Tom Shelton <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in
        news:uXr2JI9fIH A.3940@TK2MSFTN GP05.phx.gbl:
        >
        >If you have any issues let me know and I'll actually look up
        >SendMessage and test some code :)
        >
        Thanks, Tom.
        >
        I entered your code, and it seems to hang when I use SendMessage.
        I tested it here just now - and it is working. The call can take
        a few moments, because it has to notify all the top level windows on the
        system - and SendMessage will wait for all to return...

        Are us sure it is hanging permanetly?
        --
        Tom Shelton

        Comment

        • Scott McNair

          #5
          Re: Environment Variables Go &quot;Poof&quot ;

          Scott McNair <smcnair@beache xpress.takethis partout.comwrot e in
          news:Xns9A59A80 4DEA7donquixote 235gmailco@207. 46.248.16:
          Just to make sure, I started the app and let it sit there, to see how
          long it would take (if ever) to execute. So far it's been sitting for
          about ten minutes.
          As a followup:

          Rather than run the app in debug mode has I had been doing, I compiled it
          as a release, and then ran that app. It ran within a second, but it
          returned nothing in the MsgBox that supposedly is polling the environment
          variable.

          I then ran it again, and it returned the proper string in the MsgBox. I
          then went into the app and disabled the code that sets the variable,
          effectively having the MsgBox be my first line; it still reported the
          variable.

          So I decided to run it again. I deleted the value from the registry and
          reran the application. This time it reported the value properly on the
          first execution.

          I continued my experiment. I deleted the key and I remarked everything
          out. I recompiled the app, and ran it. It still reported the key. "No
          problem," I thought, "it just hasn't sent the environment refresh."

          So this time I left the registry write remarked, but I unremarked the
          SendMessage line. I ran it again, and it STILL reported C:\Program Files
          \TCS\ as the value of the variable, even though it shouldn't be there.

          The only explanation I can think of is that the SendMessage command only
          updates and inserts, but it doesn't actually clear removed items from the
          current environment.

          Does that sound about right?

          Comment

          • Scott McNair

            #6
            Re: Environment Variables Go &quot;Poof&quot ;

            Scott McNair <smcnair@beache xpress.takethis partout.comwrot e in
            news:Xns9A59AC7 52159Ddonquixot e235gmailco@207 .46.248.16:
            The only explanation I can think of is that the SendMessage command
            only updates and inserts, but it doesn't actually clear removed items
            from the current environment.
            Yet more followup:

            If I run the app and declare the variable, it does not see the variable
            within that instance of the app. I set a timer for five seconds to enable
            immediately after SendMessage, and then MsgBoxed the variable; it showed a
            blank.

            If I then uncomment the code that creates the variable, and recompile the
            app, the variable is there when I run it.

            I'm guessing that's not typical execution, but I've run it about three or
            four times, with the same result each time.

            Comment

            • Tom Shelton

              #7
              Re: Environment Variables Go &quot;Poof&quot ;

              On 2008-03-06, Scott McNair <smcnair@beache xpress.takethis partout.comwrot e:
              Scott McNair <smcnair@beache xpress.takethis partout.comwrot e in
              news:Xns9A59AC7 52159Ddonquixot e235gmailco@207 .46.248.16:
              >
              >The only explanation I can think of is that the SendMessage command
              >only updates and inserts, but it doesn't actually clear removed items
              >from the current environment.
              >
              Yet more followup:
              >
              If I run the app and declare the variable, it does not see the variable
              within that instance of the app. I set a timer for five seconds to enable
              immediately after SendMessage, and then MsgBoxed the variable; it showed a
              blank.
              >
              If I then uncomment the code that creates the variable, and recompile the
              app, the variable is there when I run it.
              >
              I'm guessing that's not typical execution, but I've run it about three or
              four times, with the same result each time.
              Scott... I can play with this a little more - but, I want to ask before
              we go much further (probably should have done this first!), but what is
              the ultimate goal here? We maybe going down the wrong road :)

              --
              Tom Shelton

              Comment

              • Scott McNair

                #8
                Re: Environment Variables Go &quot;Poof&quot ;

                Tom Shelton <tom_shelton@co mcast.netwrote in news:928f4a03-d99d-4d8b-
                93a1-3477620bb597@d6 2g2000hsf.googl egroups.com:
                Kind of rough, but it seems to work ok :) At least on XP - I suspect
                you might have permission issues on Vista ;)
                Thank you :)

                Comment

                Working...