如何在不使用“我的电脑”对话框的情况下在 Windows XP 中设置系统环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/331862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set a system environment variable in Windows XP without using the "My Computer" Dialog
提问by noahlz
I'm switching between different Java SDKs (1.4.2, 1.5.0 and 1.6.0) for various projects. I would like to set the JAVA_HOME environment variable on my Windows XP machine without going through the tedious My Computer -> Advanced -> [Select System Variable] -> Edit -> Ok -> Ok
我正在为各种项目在不同的 Java SDK(1.4.2、1.5.0 和 1.6.0)之间切换。我想在我的 Windows XP 机器上设置 JAVA_HOME 环境变量,而无需通过繁琐的我的电脑 -> 高级 -> [选择系统变量] -> 编辑 -> 确定 -> 确定
Is it possible to do this from the command line, or is there a utility that can do this?
是否可以从命令行执行此操作,或者是否有可以执行此操作的实用程序?
(Note that I am not referring to the standard batch file "SET" command - I want to set the environment variable "globally," not just for the life of a console window).
(请注意,我指的不是标准批处理文件“SET”命令——我想“全局”设置环境变量,而不仅仅是控制台窗口的生命周期)。
采纳答案by Dirk Vollmar
From http://vlaurie.com/computers2/Articles/environment.htm:
从http://vlaurie.com/computers2/Articles/environment.htm:
Using the add-on tool Setx.exe
It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. For example, to add a folder C:\New Folder to the path, the command would be setx path "%PATH%;C:\New Folder"
使用附加工具 Setx.exe
它不是标准 Windows XP 安装程序的一部分,但名为 setx.exe 的命令行工具包含在 Windows XP Service Pack 2 支持工具中。此工具扩展了 set 命令,以便可以对环境变量进行永久更改。例如,要将文件夹 C:\New Folder 添加到路径,命令将是 setx path "%PATH%;C:\New Folder"
回答by joegtp
Service Pack 2 Support Toolshas a tool called "setx.exe" that can do what you are looking for. setx path "%PATH%;C:\New Folder"
Service Pack 2 支持工具有一个名为“setx.exe”的工具,可以满足您的需求。setx 路径 "%PATH%;C:\New Folder"
回答by Patrick Cuff
Here's some VBScript I use for this:
这是我为此使用的一些 VBScript:
set args = WScript.Arguments
Set objShell = WScript.CreateObject("WScript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")
' Parse args
select case args.Count
case 0, 1, 2
help
case 3
sVariable = args(0)
sValue = args(1)
sScope = UCase(args(2))
sMode = ""
case 4
sVariable = args(0)
sValue = args(1)
sScope = UCase(args(2))
sMode = UCase(args(3))
end select
select case sScope
case "S"
if sMode = "A" then
sValue = colSystemEnvVars(sVariable) & sValue
end if
colSystemEnvVars(sVariable) = sValue
case "U"
if sMode = "A" then
sValue = colUserEnvVars(sVariable) & sValue
end if
colUserEnvVars(sVariable) = sValue
case else
help
end select
WScript.Quit
'******************************************************************************
Sub help()
WScript.Echo ""
WScript.Echo "Create or update an environment variable."
WScript.Echo ""
WScript.Echo "usage:"
WScript.Echo "======"
WScript.Echo "cscript SetVar.vbs variable value {S|U} [A]"
WScript.Echo ""
WScript.Echo "eg:"
WScript.Echo "==="
WScript.Echo "cscript SetVar.vbs MYVAR 'Hello world' U"
WScript.Echo "cscript SetVar.vbs PATH 'C:\MyPath' S A"
WScript.Quit
End Sub
The scope can be 'S'ystem or 'U'ser. The last argument, 'A', if present, appends the value to the existing value of the variable (useful for adding a directoy to the PATH system variable).
范围可以是“系统”或“用户”。最后一个参数“A”(如果存在)将值附加到变量的现有值(用于将目录添加到 PATH 系统变量)。
The variables will presist, but you'll have to close then re-open a console to use them. I usually run this from the "Run..." dialog, then open a console.
变量将保留,但您必须关闭然后重新打开控制台才能使用它们。我通常从“运行...”对话框运行它,然后打开一个控制台。