通过批处理文件传播环境变量?

发布于 2024-10-11 08:50:54 字数 428 浏览 3 评论 0原文

我正在通过批处理文件设置一些永久环境变量。是否可以在不通过批处理文件注销的情况下传播它们?我已经看过此参考,但想知道是否也可以通过批处理文件实现。

目前,当我设置环境变量(关闭 Visual Studio)并启动 Visual Studio 时,无法识别新的环境变量,并且给定项目(取决于变量)无法编译。如果我注销然后重新登录,他们就是。如果我希望批处理文件检测变量是否存在,如果存在则不执行任何操作,情况也是如此。

除非我注销/登录,否则当批处理文件设置环境变量、完成其工作并关闭时,如果我重新运行它,它将再次设置该变量(无法检测到它已经存在)。如果我注销/登录,则批处理文件会检测到该变量(并通知用户),并且 Visual Studio 会编译给定的项目,不会出现错误。

I am setting some permanent environment variables via a batch file. Is it possible to propagate them without logging off via a batch file? I have seen this reference but would like to know if it is possible via a batch file as well.

Currently, when I set the environment variables (with Visual Studio closed) and fire up Visual Studio, the new environment variables are not recognized and a given project (which depends on the variables) fails to compile. If I log-off then log back on, they are. The same is the case if I want the batch file to detect whether the variable exists and if it does, do nothing.

Unless I log-off/on, when the batch file sets the environment variable, does its job and closes, if I re-run it, it will set the variable again (fails to detect that it already exists). If I log-off/on, then the batch file detects the variable (and informs the user) and Visual Studio compiles the given project without errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-10-18 08:50:54

不确定我能遵循这个。环境变量值仅从启动新进程的进程继承。当您在 .bat 或 .cmd 文件中设置它们时,您仅更改执行批处理文件的 cmd.exe 进程的变量值。

如果您随后从同一 cmd.exe 会话启动 devenv.exe,则 Visual Studio 也将具有这些更改/添加的环境变量。

从资源管理器启动 Visual Studio 不起作用。这在您登录时就开始了。它是通过系统环境进行初始化的。您可以使用控制面板+系统小程序对其进行修改。高级,环境变量按钮。为了使此处的更改生效,您必须重新启动进程,以便使用这些更改的设置对其进行初始化。

这实际上意味着您必须重新启动资源管理器。是的,注销就可以了。

Not sure I can follow this. Environment variable values are only ever inherited from the process that starts a new process. When you set them in a .bat or .cmd file, you are altering only the variable values of the cmd.exe process that executes the batch file.

If you then start devenv.exe from that same cmd.exe session then Visual Studio will have those altered/added environment variables as well.

What does not work is starting Visual Studio from Explorer. That got started way back when you logged in. It got initialized with the system environment. Which you can modify with the Control Panel + System applet. Advanced, Environment variables button. To make the changes here effective, you have to restart a process so it gets initialized with these altered settings.

Which in effect means that you have to restart Explorer. Yes, logging out does that.

夜空下最亮的亮点 2024-10-18 08:50:54

问题:您在 .bat 文件中设置了持久环境变量,并且想知道是否可以在不注销的情况下运行 Visual C++ /
回收探索者?

因此,您正在使用 SETX 从 .bat 文件内创建持久环境变量。如果您随后在同一个 bat 文件中使用 SET 为本地 cmd shell 设置相同的环境值,那么它们将可以立即从本地 cmd shell 中使用。

然后创建另一个批处理文件来启动 Visual C++,并使用它从运行原始 .bat 文件的同一个 shell 启动 Visual C++。
要从命令中启动 Visual C++,请键入 [start devenv]。

我不确定你的工作流程,但权衡是你为批处理文件中的每个环境变量设置了 setx 和镜像,而不是重新登录或记住循环 cmdshell 或资源管理器进程。

如果批处理文件是一次写入就忘记的,我会考虑 setx/set 解决方案。

Problem: You set persistent environment variables in your .bat file and are wondering if you can run Visual C++ without logging off /
recycling explorer ?

So you are using SETX to create persistent environment variables from inside a .bat file. if you then inside the same bat file use SET to set the same Environment Values for the local cmd shell then they will be useable immediately from the local cmd shell.

Then create another batch file to start Visual C++ and use it to start visual C++ from the same shell you ran your original .bat file from.
To bring Visual C++ up from the command like you type [start devenv].

I'm not sure of your workflow but the trade off is you have have both setx and a mirror set for each environment variable in your batch file, against relogging on or remembering to cycle your cmdshell or explorer process.

If the batch file is a write once and forget, I'd think about the setx/set solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文