admin管理员组文章数量:1631138
本文翻译自:Adding directory to PATH Environment Variable in Windows
I am trying to add C:\\xampp\\php
to my system PATH
environment variable in Windows. 我试图将C:\\xampp\\php
添加到Windows中的系统PATH
环境变量中。
I have already added it using the Environment Variables dialog box. 我已经使用“环境变量”对话框添加了它。
But when I type into my console: 但是当我输入控制台时:
C:\>path
it doesn't show the new C:\\xampp\\php
directory: 它不显示新的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
I have two questions: 我有两个问题:
- Why did this happen? 为什么会这样呢? Is there something I did wrong? 我做错了什么吗?
- Also, how do I add directories to my
PATH
variable using the console (and programmatically, with a batch file)? 另外,如何使用控制台(以编程方式,使用批处理文件)将目录添加到PATH
变量中?
#1楼
参考:https://stackoom/question/e3Qy/在Windows中将目录添加到PATH环境变量
#2楼
WARNING: This solution may be destructive to your PATH, and the stability of your system. 警告:此解决方案可能会对您的PATH和系统的稳定性造成破坏 。 As a side effect, it will merge your user and system PATH, and truncate PATH to 1024 characters. 副作用是,它将合并用户和系统PATH,并将PATH截断为1024个字符。 The effect of this command is irreversible. 此命令的作用是不可逆的。 Make a backup of PATH first. 首先备份PATH。 See the comments for more information. 请参阅注释以获取更多信息。
Don't blindly copy-and-paste this. 不要盲目复制并粘贴此内容。 Use with caution. 请谨慎使用。
You can permanently add a path to PATH
with the setx
command: 您可以使用setx
命令将路径永久添加到PATH
:
setx /M path "%path%;C:\your\path\here\"
Remove the /M
flag if you want to set the user PATH
instead of the system PATH
. 如果要设置用户PATH
而不是系统PATH
删除/M
标志。
Notes: 笔记:
- The
setx
command is only available in Windows 7 and later.setx
命令仅在Windows 7和更高版本中可用。 You should run this command from an elevated command prompt. 您应该从提升权限的命令提示符下运行此命令。
If you only want to change it for the current session, use set . 如果只想在当前会话中更改它,请使用set 。
#3楼
I would use PowerShell instead! 我会改用PowerShell!
To add a directory to PATH using PowerShell, do the following: 要使用PowerShell将目录添加到PATH,请执行以下操作:
$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path")
To set the variable for all users, machine-wide, the last line should be like: 要在整个计算机范围内为所有用户设置变量,最后一行应类似于:
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
In a PowerShell script, you might want to check for the presence of your C:\\xampp\\php
before adding to PATH (in case it has been previously added). 在PowerShell脚本中,您可能需要在添加到PATH之前检查C:\\xampp\\php
的存在(如果先前已添加)。 You can wrap it in an if
conditional. 您可以将其包装在if
条件中。
So putting it all together: 所以放在一起:
$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
if( $PATH -notlike "*"+$xampp_path+"*" ){
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
}
Better still, one could create a generic function. 更好的是,可以创建一个泛型函数。 Just supply the directory you wish to add: 只需提供您要添加的目录:
function AddTo-Path{
param(
[string]$Dir
)
if( !(Test-Path $Dir) ){
Write-warning "Supplied directory was not found!"
return
}
$PATH = [Environment]::GetEnvironmentVariable("PATH")
if( $PATH -notlike "*"+$Dir+"*" ){
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$Dir", "Machine")
}
}
You could make things better by doing some polishing. 您可以做一些抛光来使事情变得更好。 For example, using Test-Path
to confirm that your directory actually exists. 例如,使用Test-Path
确认您的目录确实存在。
#4楼
You don't need any set
or setx
command, simply open the terminal and type: 您不需要任何set
或setx
命令,只需打开终端并输入:
PATH
This shows the current value of PATH variable. 这显示了PATH变量的当前值。 Now you want to add directory to it? 现在要添加目录吗? Simply type: 只需键入:
PATH %PATH%;C:\xampp\php
If for any reason you want to clear the PATH variable (no paths at all or delete all paths in it), type: 如果出于任何原因要清除PATH变量(根本没有路径或删除其中的所有路径),请键入:
PATH ;
Update 更新资料
Like Danial Wilson noted in comment below, it sets the path only in current session. 就像Danial Wilson在下面的评论中指出的那样,它仅在当前会话中设置路径。 To set the path permanently use setx
but be aware, although that sets the path permanently but NOT in the current session, so you have to start a new command line to see the changes, more info here . 要永久设置路径,请使用setx
但请注意,尽管这将永久设置路径,但不是在当前会话中设置路径,所以您必须启动新命令行以查看更改,更多信息在此处 。
To check if an environmental variable exist or see its value use ECHO commnad: 要检查环境变量是否存在或查看其值,请使用ECHO命令:
echo %YOUR_ENV_VARIABLE%
#5楼
Regarding point 2 I'm using a simple batch file that is populating PATH
or other environment variables for me. 关于第2点,我正在使用一个简单的批处理文件,该文件正在为我填充PATH
或其他环境变量。 Therefore, there is no pollution of environment variables by default. 因此,默认情况下不会污染环境变量。 This batch file is accessible from everywhere so I can type: 可以从任何地方访问此批处理文件,因此我可以输入:
c:\>mybatchfile
-- here all env. are available
c:\>php file.php
#6楼
Aside from all the answers, if you want a nice GUI tool to edit your windows environment variables you can use Rapid Environment Editor 除了所有答案之外,如果您想要一个不错的GUI工具来编辑Windows环境变量,则可以使用Rapid Environment Editor
try it! 试试吧! its safe to use and awesome! 它的使用安全和超赞!
http://www.rapidee/en/ http://www.rapidee/en/
版权声明:本文标题:在Windows中将目录添加到PATH环境变量 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729087719a1185803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论