windows 如何使用 BATCH 或 VBS 制作永久系统变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8705358/
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 to make permanent system variable using BATCH or VBS?
提问by klaatu
I am making an installer which will be 100% depending on system variable such as %TEMP% or %Path%, users first double click windows.bat
file.
我正在制作一个安装程序,它将 100% 取决于系统变量,例如 %TEMP% 或 %Path%,用户首先双击windows.bat
文件。
But, How can i setup permanent system variable in Windows XP, Vista, 7, 8 using VBS or BATCH?
但是,如何使用 VBS 或 BATCH 在 Windows XP、Vista、7、8 中设置永久系统变量?
I tried with BATCH but in Windows XP, most users do not have setx
by default, so i want to avoid using that technique. Is there better way to do that?
我尝试使用 BATCH 但在 Windows XP 中,大多数用户setx
默认情况下没有,所以我想避免使用该技术。有没有更好的方法来做到这一点?
C:\Documents and Settings\sun>REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Ses
sion Manager\Environment" /v MyApplicationWillUsetThis /d "C:\WhatEverPathToIncl
udeHereQuestionMark"
The operation completed successfully
C:\Documents and Settings\sun>REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Ses
sion Manager\Environment" /v MyApplicationWillUsetThis /d "C:\WhatEverPathToIncl
udeHereQuestionMark"
Value MyApplicationWillUsetThis exists, overwrite(Y/N)? Y
The operation completed successfully
回答by Norbert Willhelm
You can create a REG_SZ value under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment in the registry. The name of the value specifies the name of the environment variable, the value specifies the value of the environment variable.
您可以在注册表中的 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 下创建一个 REG_SZ 值。值的名称指定环境变量的名称,值指定环境变量的值。
You can also modify existing values.
您还可以修改现有值。
In order to modify the registry you can use the RegRead and RegWrite method of the WScript.Shell object. For an example have a look at Manipulating the System Registry.
为了修改注册表,您可以使用 WScript.Shell 对象的 RegRead 和 RegWrite 方法。例如,查看操作系统注册表。
Edit:You could at first delete the existing value and recreate it afterwards.
编辑:您可以先删除现有值,然后再重新创建。
REG DELETE "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyApplicationWillUsetThis /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyApplicationWillUsetThis /d "C:\WhatEverPathToIncludeHereQuestionMark"
回答by klaatu
How to set environment variables from VBScript
如何从 VBScript 设置环境变量
Set WshShell = CreateObject("Wscript.Shell")
' set a permanent environment variable %MyVarName% for all users
WshShell.Environment.item("MyVarName") = "MyVarValue"
If the above setting isn't permanent, try this one. This will set a permanent environment variable for all users. You can try System, User, Volatile, and Process categories with the Environment collection.
如果上述设置不是永久性的,请尝试此设置。这将为所有用户设置一个永久的环境变量。您可以使用 Environment 集合尝试 System、User、Volatile 和 Process 类别。
With WSHShell.Environment("SYSTEM")
.item("MyVarName") = "MyVarValue"
End With
You can also use ExpandEnvironmentStrings to read environment variables or replace them with their values in long command line type strings.
您还可以使用 ExpandEnvironmentStrings 读取环境变量或将它们替换为长命令行类型字符串中的值。
sValue = WshShell.ExpandEnvironmentStrings("%MyVarName%")
To work with the registry try
要使用注册表尝试
sRegKey = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyVarName"
sValue = WshShell.RegRead(sRegKey)
' don't write if no change is required.
If sValue <> sNewValue Then WshShell.RegWrite sRegKey , sNewValue