Windows 环境变量是如何评估的?

发布于 2024-10-19 14:48:53 字数 179 浏览 1 评论 0原文

如果我有一个同名的 SystemUser 环境变量,它们是如何处理的?它们是串联的吗? user 变量是否会覆盖 system 变量?考虑到这一点,如果我需要向 Path 变量添加一些内容,在哪里添加更方便?

If I have a System and User environment variable with the same name, how are they processed? Are they concatenated? Does the user variable override the system variable? Taking that into account, if I need to add something to the Path variable, where is it more convenient to add it?

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

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

发布评论

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

评论(3

半﹌身腐败 2024-10-26 14:48:54

飞溅西蒙<的一切/a> 说他们的答案是正确的。附加用户路径变量的想法已经被强调,我相信这种差异的后果需要一些额外的处理。

路径 = %Path%(系统); %路径%(用户)

当您执行可执行程序(或任何可执行脚本,例如.bat.vbs等)时,您不需要提供完全合格的路径。

例如,要运行 java,您可以输入以下任意内容:

C:/Program Files (x86)/Java/jre6/bin/java -version

java.exe -version

java -version

第一个示例使用完全限定路径。这将始终使用该确切路径上的 Java 版本。

第二个示例将遍历 %Path% 环境变量中的每个目录,查找名为 java.exe 的可执行文件。它将运行找到的第一个,然后停止搜索。 如果 %Path% 上有两个名为 java.exe 的文件,则仅使用找到的第一个文件。

第三个示例,例如第二个,将迭代 %Path% 中列出的目录。此外,由于未提供文件扩展名,因此将按照 %PATHEXT% 环境变量中指定的顺序将可执行文件扩展名列表附加到文件名。 如果上的某处有多个名为java.comjava.exejava.bat等的文件%Path%,仅使用找到的第一个。

您可以通过创建以下批处理文件来查看系统上的可执行路径扩展列表:

@echo off
echo %PATHEXT%
pause

在我的计算机上,这些是:

.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

这一切是什么意思?

与其他环境变量形成鲜明对比,用户路径不允许您覆盖系统路径。情况恰恰相反。从上面的示例中可以看出,在很多情况下您可能希望更改 Java 的默认版本。但是,如果系统路径中已经列出了 Java 版本,则始终会首先找到该版本,因为路径是按从左到右的顺序搜索的,用户路径附加在右侧侧,系统路径位于左侧。

我能做什么呢?

如果您无权访问系统环境变量,则无法使用用户路径覆盖系统路径上的默认程序。 (事实上​​,它必须是这样,否则某些程序将停止正常工作,并且会使您的系统容易被恶意软件篡改。没有人希望这样。)

相反,您必须使用完全限定的路径如果您必须使用特定版本。

Everything that splash and Simon say in their answers are correct. The idea that the user path variable is appended has been highlighted, and I believe the consequences of that difference require some additional treatment.

Path = %Path% (System) ; %Path% (User)

When you execute an executable program (or any executable script, such as .bat, .vbs, etc.) you do not need to provide the fully qualified path.

For instance, to run java, you can type in any of these:

C:/Program Files (x86)/Java/jre6/bin/java -version

java.exe -version

java -version

The first example uses a fully qualified path. This will always use the version of the Java at that exact path.

The second example will go through each of the directories in the %Path% environment variable, looking for an executable file named java.exe. It will run the very first one that is found, and stop searching. If there are two files named java.exe somewhere on the %Path%, only the first one found is used.

The third example, like the second, will iterate over the directories listed in the %Path%. In addition, because a file extension was not provided, a list of executable file extensions are appended to the name of the file, in the order specified in the %PATHEXT% environment variable. If there are several files named java.com, java.exe, java.bat, etc. somewhere on the %Path%, only the first one found is used.

You can see the list of executable path extensions on your system by creating the following batch file:

@echo off
echo %PATHEXT%
pause

On my machine, these are:

.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

What does all this mean?

In stark contrast to other environment variable, the user path does not allow you to override the system path. The exact opposite is the case. From the examples above, there are many cases where you may wish to change the default version of Java. However, if there is already a Java version listed in the system path, that is the version that will ALWAYS be found first, because the path is searched in order, left-to-right, with the user path appended on the right-hand side, with the system path on the left.

What can I do about it?

If you do not have access to system environment variables, you cannot override default programs on the system path by using the user path. (In fact, it must be this way, or certain programs would stop working correctly, and would open your system to tampering by malicious software. Nobody wants that.)

Instead, you must use a fully qualified path if you must use a specific version.

旧瑾黎汐 2024-10-26 14:48:53

splash 中所说的一切他们的答案 是正确的。需要明确的是,用户路径环境变量的评估方式与其他用户环境变量的评估方式之间存在差异。常规用户环境变量将完全覆盖同名的系统环境变量(如果两者都存在),但仅限于为其指定的特定用户。 但是,用户路径变量的处理方式有所不同。它在评估时附加到系统路径变量,而不是完全替换它。我相信splash指出了这一点,但他们做得如此简洁,我认为需要拼写清楚。

Everything splash says in their answer is correct. To be absolutely clear, there is a difference between how the user path environment variable is evaluated, and the other user environment variables are evaluated. A regular user environment variable overrides completely a system one with the same name if both exist, but only for the specific user it is specified for. However, the user path variables is treated differently. It is appended to the system path variable when evaluating, rather than completely replacing it. I believe splash states that, but they do it so concisely I think it needs spelling out.

情感失落者 2024-10-26 14:48:53

我认为这篇文章应该回答您的问题:Windows NT 中的环境变量

用户环境变量

用户环境变量可以是
也可以从控制面板查看。这
用户可以添加、删除或修改
用户中的环境变量
用户字段的环境变量。
这些变量优先于
系统环境变量。用户
路径附加到系统路径。

I think this article should answer you question: Environment variables in Windows NT

User environment variables

User environment variables can be
viewed from Control Panel as well. The
user may add, delete or modify the
environment variables in the User
Environment Variables for User field.
These variables take precedence over
system environment variables. The user
path is appended to the system path.

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