博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Windows中将目录添加到PATH环境变量
阅读量:2289 次
发布时间:2019-05-09

本文共 5318 字,大约阅读时间需要 17 分钟。

本文翻译自:

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 SQLServer\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: 我有两个问题:

  1. Why did this happen? 为什么会这样呢? Is there something I did wrong? 我做错了什么吗?
  2. Also, how do I add directories to my PATH variable using the console (and programmatically, with a batch file)? 另外,如何使用控制台(以编程方式,使用批处理文件)将目录添加到PATH变量中?

#1楼

参考:


#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 command: 您可以使用命令将路径永久添加到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 . 如果只想在当前会话中更改它,请使用 。


#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: 您不需要任何setsetx命令,只需打开终端并输入:

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 . 要永久设置路径,请使用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 availablec:\>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 除了所有答案之外,如果您想要一个不错的GUI工具来编辑Windows环境变量,则可以使用

try it! 试试吧! its safe to use and awesome! 它的使用安全和超赞!

转载地址:http://zpcnb.baihongyu.com/

你可能感兴趣的文章
IO流创建java文件列表
查看>>
Properties存取配置文件
查看>>
记录应用程序运行次数
查看>>
打印流PrintStream和PrintWriter
查看>>
IO流 合并流 SequenceInputStream
查看>>
IO流切割文件
查看>>
IO流-对象序列化操作流
查看>>
io流-多线程连接管道流
查看>>
RandomAccessFile可实现数据的分段写入也就是多线程下载
查看>>
DataInputStream与DataOutputStream用于操作基本数据类型的数据的流对象
查看>>
ByteArrayStream用于操作字节数组的流对象
查看>>
IO流-字符编码表转换示例
查看>>
IO流-转换流的字符编码转换-ISO-8859-1和utf-8和GBK互转
查看>>
网络游戏线上活动的类型及特点(二)
查看>>
sleeping-to-save-cpu
查看>>
键盘符号英语读法
查看>>
[转]char_traits
查看>>
[转载] 人生三重境界
查看>>
[转载]学习时注意思考方法
查看>>
C10K问题
查看>>