监视对环境变量的更改 [英] Monitor changes to an environment variable

查看:62
本文介绍了监视对环境变量的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的英语不好.

我有两个应用程序A和B.应用程序A设置了一个环境变量,而应用程序B可以更改此变量.我想知道App-A是否有可能监视环境变量的变化.我一直在寻找FileSystemWatcher之类的东西,只是我想监视环境变量而不是文件系统.而且我想避免在App-A中有一个工作线程定期轮询该值以查找更改.

解决方案

据我所知,没有操作系统提供监视环境变量更改的方法.大多数应用程序在启动时都会读取这些变量,并且直到下一次运行时才会对更改进行处理.不过,您可以尝试一些解决方法.在Windows的最新版本中,环境变量会保留在注册表中.因此,您可以直接检查/监视以下两个注册表项,而不是直接监视更改:

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


关于如何监视注册表,WMI提供了一个RegistryValueChangeEvent 类,可用于监视对特定键或值的更改.但是,如果我是您,那么我将重新考虑设计,并基本希望该应用一旦启动就不会受到环境变量更改的影响.


您更改变量后,App-B可能会广播WM_SETTINGCHANGE消息-检查文档中的WM_SETTINGCHANGE并在此处进行注释: http: //support.microsoft.com/kb/104011 [^ ].

在App-A中为该消息编写处理程序比轮询更改要容易,但是当用户使用命令提示符中的set命令更改内容时,可能无法处理这种情况.


<您可以使用shell或进程并执行DOS shell命令:set and echo此处提供更多信息: http://en.wikipedia.org /wiki/Environment_variable 或> WinAPI http://msdn.microsoft.com/en-us/library/ms683188( VS.85).aspx 并添加计时器进行检查! (也许可以为您提供帮助)


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.

解决方案

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.


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.


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)


这篇关于监视对环境变量的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆