我试图在Windows中添加C:\xampp\php到我的系统PATH环境变量。

我已经使用环境变量对话框添加了它。

但当我在控制台输入:

C:\>path

它不会显示新的C:\xampp\php目录:

PATH=D:\Program Files\Autodesk\Maya2008\bin;C:\Ruby192\bin;C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;C:\PROGRA~1\DISKEE~2\DISKEE~1\;c:\Program Files\Microsoft SQL
Server\90\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;D:\Program Files\TortoiseSVN\bin
;D:\Program Files\Bazaar;C:\Program Files\Android\android-sdk\tools;D:\Program Files\
Microsoft Visual Studio\Common\Tools\WinNT;D:\Program Files\Microsoft Visual Studio\Common
\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\
Microsoft Visual Studio\VC98\bin

我有两个问题:

为什么会发生这种情况?我做错什么了吗? 另外,如何使用控制台(并以编程方式,使用批处理文件)向PATH变量添加目录?


当前回答

命令行更改将不是永久的,并且将在控制台关闭时丢失。 这条路就像先到先得一样。 您可能希望覆盖其他已经包含的可执行文件。例如,如果您的路径上已经有了另一个版本,而您希望添加不同的版本,而不需要对路径进行永久更改,则应该将目录放在命令的开头。

重写已包含的可执行文件;

设置路径= C: \ xampp \ php; % %路径;

其他回答

控制面板的一个更好的选择是使用SourceForge的免费程序Pathenator。

但是,它只适用于。net 4.0或更高版本的系统,如Windows 7、Windows 8或Windows 10。

在命令提示符中,您告诉Cmd使用Windows资源管理器的命令行,在它前面加上start。

因此,启动Yourbatchname。

注意,您必须注册,如果它的名称是batchfile.exe。

可以将程序和文档添加到注册表中,因此在“开始-运行”对话框或快捷方式中输入它们的名称而不输入路径,Windows就可以找到它们。

这是一个通用的reg文件。复制下面的行到一个新的文本文档,并将其保存为anyname.reg。用你的程序或文档编辑它。

在路径中,使用\\分隔键路径中的文件夹名称,就像regedit使用单个\分隔键名称一样。所有reg文件都以REGEDIT4开头。分号将一行转换为注释。符号@表示将值赋给键,而不是指定值。

该文件不必存在。这可以用来设置Word.exe打开Winword.exe。

输入start batchfile将启动iexplorer .exe。

REGEDIT4
;The bolded name below is the name of the document or program, <filename>.<file extension>

[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\Batchfile.exe]

; The @ means the path to the file is assigned to the default value for the key.
; The whole path in enclosed in a quotation mark ".

@="\"C:\\Program Files\\Internet Explorer\\iexplore.exe\""

; Optional Parameters. The semicolon means don't process the line. Remove it if you want to put it in the registry

; Informs the shell that the program accepts URLs.

;"useURL"="1"

; Sets the path that a program will use as its' default directory. This is commented out.

;"Path"="C:\\Program Files\\Microsoft Office\\Office\\"

你已经在另一个答案中知道了路径。也参见doskey /?对于CMD宏(它们只在输入时工作)。

您可以在CMD下执行启动命令。来自Windows资源包技术参考

自动运行

HKCU\Software\Microsoft\Command Processor

Data type Range Default value
REG_SZ  list of commands  There is no default value for this entry.

描述

包含每次启动Cmd.exe时执行的命令。

关于第2点,我使用了一个简单的批处理文件,它为我填充PATH或其他环境变量。因此,默认情况下不存在任何环境变量的污染。这个批处理文件可以从任何地方访问,所以我可以输入:

mybatchfile

输出:

-- Here all environment variables are available

And:

php file.php

如果你已经在你想添加到PATH的目录中,这很方便:

set PATH=%PATH%;%CD%

它适用于标准的Windows cmd,但不适用于PowerShell。

对于PowerShell, %CD%等价于[System.Environment]::CurrentDirectory。

在Windows 10上,我能够搜索set path环境变量,并得到这些指令:

From the desktop, right-click the very bottom-left corner of the screen to get the Power User Task Menu. From the Power User Task Menu, click System. In the Settings window, scroll down to the Related settings section and click the System info link. In the System window, click the Advanced system settings link in the left navigation panel. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab. In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below:

C:\程序文件;C:\Winnt;C:\Winnt\System32

我第一次搜索它时,它立即弹出了系统属性窗口。之后,我找到了上面的说明。