我正在制作一个安装程序,它将 100% 取决于系统变量,例如 %TEMP% 或 %Path%,用户首先双击 windows.bat
文件。
但是,如何使用 VBS 或 BATCH 在 Windows XP、Vista、7、8 中设置永久系统变量?
我尝试使用 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
最佳答案
您可以在注册表的 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 下创建一个 REG_SZ 值。值的名称指定环境变量的名称,值指定环境变量的值。
您还可以修改现有值。
为了修改注册表,您可以使用 WScript.Shell 对象的 RegRead 和 RegWrite 方法。举个例子看看 Manipulating the System Registry .
编辑:您可以先删除现有值,然后再重新创建。
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"
关于windows - 如何使用 BATCH 或 VBS 创建永久系统变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8705358/