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. Environment Variables not Updating

Environment Variables not Updating

Scheduled Pinned Locked Moved C#
tutorialcsharphelpquestionannouncement
4 Posts 2 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.
  • J Offline
    J Offline
    jgallen23
    wrote on last edited by
    #1

    For some reason the Environment.GetEnvironmentVariable("PATH") isn't reflecting the changes I've made in the control panel. If I restart, they return correctly. For example, if I add c:\testpath to my environment variables in window xp control panel, and then write a sample c# app to show the path (Console.WriteLine(Environment.GetEnvironmentVariable("PATH"));), the c:\testpath doesn't show up. anybody know how to fix this or if there is a way to update my environment variables?

    A 1 Reply Last reply
    0
    • J jgallen23

      For some reason the Environment.GetEnvironmentVariable("PATH") isn't reflecting the changes I've made in the control panel. If I restart, they return correctly. For example, if I add c:\testpath to my environment variables in window xp control panel, and then write a sample c# app to show the path (Console.WriteLine(Environment.GetEnvironmentVariable("PATH"));), the c:\testpath doesn't show up. anybody know how to fix this or if there is a way to update my environment variables?

      A Offline
      A Offline
      Ami Bar
      wrote on last edited by
      #2

      First processes environment variables are derived from its parent process. Second, once the environment variables are changed outside the application it will not effect running processes. If you change an environment variables while your application is running you won’t see it. If you change an environment variables while the parent process of your application is running you won’t see the change when your application runs. The later applies when you run your application in debug mode from the Visual Studio. If you run your application from the explorer (double click on it) it will get the correct environment variable. I know that when an environment variable is changed, Windows send a broadcast Windows’ message that it happened. Ami

      J 1 Reply Last reply
      0
      • A Ami Bar

        First processes environment variables are derived from its parent process. Second, once the environment variables are changed outside the application it will not effect running processes. If you change an environment variables while your application is running you won’t see it. If you change an environment variables while the parent process of your application is running you won’t see the change when your application runs. The later applies when you run your application in debug mode from the Visual Studio. If you run your application from the explorer (double click on it) it will get the correct environment variable. I know that when an environment variable is changed, Windows send a broadcast Windows’ message that it happened. Ami

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

        Ami, Thanks, that makes a lot of sense. The problem I was having is that my application stays open the entire time and won't register the new path until it's restarted. Is there any way to programatically update the enviornment variables?

        A 1 Reply Last reply
        0
        • J jgallen23

          Ami, Thanks, that makes a lot of sense. The problem I was having is that my application stays open the entire time and won't register the new path until it's restarted. Is there any way to programatically update the enviornment variables?

          A Offline
          A Offline
          Ami Bar
          wrote on last edited by
          #4

          If you have WinForm application you can override the WndProc method:

          public class Form1 : Form
          {
          private const int WM_SETTINGCHANGE = 0x001A;

          public Form1()
          {
              InitializeComponent();
          }
          
          protected override void WndProc(ref Message m)
          {
              // This is a message code that is sent when system-wide setting is changed
              if (WM\_SETTINGCHANGE == m.Msg)
              {
                  if (m.LParam != IntPtr.Zero)
                  {
                      // Convert the LParam to string 
                      string whatChanged = System.Runtime.InteropServices.Marshal.PtrToStringAuto(m.LParam);
          
                      // Check if an Environment variable was changed 
                      if (null != whatChanged && "Environment" == whatChanged)
                      {
                          // Here goes the code that updates your application
                          MessageBox.Show("Environment variable changed");
                      }
                  }
              }
              // Don't forget to execute the message so the GUI will work.
              base.WndProc(ref m);
          }
          

          }

          Ami

          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