Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello everybody,

My English isn't good.

I have two apps A and B. App-A sets an environment variable, and App-B can potentially change this variable. I was wondering if it's possible for App-A to watch for changes to the environment variable. I was looking for something like the FileSystemWatcher except that I want to monitor environment variables rather than the file system. And I want to avoid having a worker thread in App-A that periodically polls the value to look for changes.
Posted
Updated 27-Jun-10 12:01pm
v3

As far as I know, there's no OS provided way to monitor changes to environment variables. Most apps read these variables at startup and don't bother with changes till the next time they are run. There is something you could try as a workaround though. In recent versions of Windows, environment variables are persisted in the registry. So instead of monitoring for changes directly, you could examine/monitor the following two registry keys:

  • HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
  • HKEY_CURRENT_USER\Environment


And as to how you can monitor the registry, WMI provides a RegistryValueChangeEvent class that you can use to monitor changes to a specific key or value. But if I were you, I'd rethink my design and make it a basic expectation that the app will not be affected by environment variable changes once it's been started.
 
Share this answer
 
v3
You might have App-B broadcast a WM_SETTINGCHANGE message after a variable is changed - check the docs for WM_SETTINGCHANGE and notes here: http://support.microsoft.com/kb/104011[^].

It would be easier to code a handler for this message in App-A than to poll for changes, but might not handle the case when a user changes things with a set command from the command prompt.
 
Share this answer
 
you can use shell or process and execute DOS shell commands : set and echo more information here : http://en.wikipedia.org/wiki/Environment_variable or > WinAPI http://msdn.microsoft.com/en-us/library/ms683188(VS.85).aspx and add timer to check it! (maybe can help you)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900