我试图在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变量添加目录?


当前回答

除了这些答案,如果你想要一个很好的GUI工具来编辑你的Windows环境变量,你可以使用快速环境编辑器。

试一试!它使用起来很安全,而且很棒!

其他回答

这只是修改注册表。现有的进程不会使用这些值。如果一个新进程在此更改之后启动,并且不从其父进程继承旧环境,那么它将这样做。

您没有指定如何启动控制台会话。确保这一点的最佳方法是退出命令shell并再次运行它。然后它应该继承更新后的PATH环境变量。

我会用PowerShell代替!

使用PowerShell将一个目录添加到PATH,执行以下操作:

$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path")

要为机器范围内的所有用户设置变量,最后一行应该如下所示:

[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")

在PowerShell脚本中,您可能希望在添加到PATH之前检查是否存在C:\xampp\php(以防之前已经添加了它)。你可以用if条件句把它包装起来。

所以把它们放在一起:

$PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine")
$xampp_path = "C:\xampp\php"
if( $PATH -notlike "*"+$xampp_path+"*" ){
    [Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
}

更好的是,可以创建一个泛型函数。只需提供您希望添加的目录:

function AddTo-Path{
param(
    [string]$Dir
)

    if( !(Test-Path $Dir) ){
        Write-warning "Supplied directory was not found!"
        return
    }
    $PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine")
    if( $PATH -notlike "*"+$Dir+"*" ){
        [Environment]::SetEnvironmentVariable("PATH", "$PATH;$Dir", "Machine")
    }
}

你可以通过打磨来让事情变得更好。例如,使用Test-Path确认目录确实存在。

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

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

在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

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

使用gtools中的path。

它以一种直观的方式做事。例如:

pathed /REMOVE "c:\my\folder"
pathed /APPEND "c:\my\folder"

它显示结果,而不需要衍生一个新的cmd!