通过Windows资源管理器传递环境变量

时间:2022-01-24 23:17:51

I want to set an environment variable, and then spawn a Windows Explorer window in such a way that processes launched from this new Explorer window inherit that environment variable. I want to do this, so that a diff tool I am integrating with the TortoiseSVN shell extension has access to project-specific environment variables set in a project startup script. But the obvious ways of doing this seem to lose the environment variable somewhere:

我想设置一个环境变量,然后生成一个Windows资源管理器窗口,使得从这个新的Explorer窗口启动的进程继承该环境变量。我想这样做,因此我与TortoiseSVN shell扩展集成的diff工具可以访问项目启动脚本中设置的项目特定的环境变量。但显而易见的方法似乎在某处失去了环境变量:

  1. launch cmd.exe
  2. set MYVAR="foo"
  3. set | findstr MYVAR
  4. 设置|找到MYVAR

  5. Result contains MYVAR="foo"
  6. 结果包含MYVAR =“foo”

  7. explorer .
  8. From the resulting Explorer window, Shift+Right-click a folder and choose "Open Command Window Here"
  9. 在生成的资源管理器窗口中,按住Shift键并右键单击文件夹,然后选择“在此处打开命令窗口”

  10. set | findstr MYVAR
  11. 设置|找到MYVAR

  12. Result does not contain MYVAR
  13. 结果不包含MYVAR

I have set my Windows Explorer settings to spawn a new process for each Explorer window, hoping that would help, but it seems to have no effect.

我已经设置了我的Windows资源管理器设置为每个资源管理器窗口生成一个新进程,希望这会有所帮助,但它似乎没有任何效果。

I can use setx instead of set to store the variable globally, however if I want multiple windows to have different versions of MYVAR (e.g. if I'm working on multiple projects at once), this will not work.

我可以使用setx而不是set来全局存储变量,但是如果我想让多个窗口拥有不同版本的MYVAR(例如,如果我一次处理多个项目),这将无效。

Is there a way to set an environment variable specific to a Windows Explorer window, which will be inherited by all its child processes?

有没有办法设置特定于Windows资源管理器窗口的环境变量,它将由其所有子进程继承?

2 个解决方案

#1


  1. Open CMD

    setx foo bar

    setx foo吧

  2. Start explorer

    start .

  3. 启动资源管理器启动。

  4. Open a new command window (Shift+Right-click a folder)

    打开一个新的命令窗口(Shift +右键单击一个文件夹)

  5. type

    set foo

  6. 键入set foo

  7. Output

    foo=bar

  8. Now if you go back to the cmd opened in 2 and type

    现在,如果你回到2中打开的cmd并输入

    setx foo bar2

    setx foo bar2

the CMD opened in 3 will still show

CMD开放的CMD仍将显示

>set foo
foo=bar

But if you open a new CMD.EXE from an explorer from the CMD opened in 6 you will get

但是,如果您从6中打开的CMD中的资源管理器中打开一个新的CMD.EXE,您将获得

>set foo
foo=bar2

This works because SETX passes variables created or modified to future command windows but not in the current or already created CMD.exe.

这是有效的,因为SETX将创建或修改的变量传递给将来的命令窗口,但不会传递给当前或已创建的CMD.exe。

#2


A program gets a copy of it's starting process environment variables. So this works if you a spawning a new process (a setting in Folder Options - View).

程序获取它的起始进程环境变量的副本。因此,如果您生成一个新进程(“文件夹选项 - 视图”中的设置),则此方法有效。

Set a=fred
explorer.exe

set a=cat
explorer.exe

and remember you have to specify the /n command line parameters to force two windows at one folder.

并记住你必须指定/ n命令行参数来强制在一个文件夹中的两个窗口。

#1


  1. Open CMD

    setx foo bar

    setx foo吧

  2. Start explorer

    start .

  3. 启动资源管理器启动。

  4. Open a new command window (Shift+Right-click a folder)

    打开一个新的命令窗口(Shift +右键单击一个文件夹)

  5. type

    set foo

  6. 键入set foo

  7. Output

    foo=bar

  8. Now if you go back to the cmd opened in 2 and type

    现在,如果你回到2中打开的cmd并输入

    setx foo bar2

    setx foo bar2

the CMD opened in 3 will still show

CMD开放的CMD仍将显示

>set foo
foo=bar

But if you open a new CMD.EXE from an explorer from the CMD opened in 6 you will get

但是,如果您从6中打开的CMD中的资源管理器中打开一个新的CMD.EXE,您将获得

>set foo
foo=bar2

This works because SETX passes variables created or modified to future command windows but not in the current or already created CMD.exe.

这是有效的,因为SETX将创建或修改的变量传递给将来的命令窗口,但不会传递给当前或已创建的CMD.exe。

#2


A program gets a copy of it's starting process environment variables. So this works if you a spawning a new process (a setting in Folder Options - View).

程序获取它的起始进程环境变量的副本。因此,如果您生成一个新进程(“文件夹选项 - 视图”中的设置),则此方法有效。

Set a=fred
explorer.exe

set a=cat
explorer.exe

and remember you have to specify the /n command line parameters to force two windows at one folder.

并记住你必须指定/ n命令行参数来强制在一个文件夹中的两个窗口。