windows - 如何使用 BATCH 或 VBS 创建永久系统变量?

标签 windows windows-7 vbscript batch-file

我正在制作一个安装程序,它将 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/

相关文章:

python - 无法在 Windows 7 中安装图形工具

vbscript - On Error 语句中的错误

.net - Wix:将自定义操作中的文件复制到程序目录(Windows 7)

c - 已安装检查 c 但未找到 "check.h"

python - #include <Python.h> Windows 10 修复没有这样的文件或目录

vbscript - Windows 脚本宿主 - (文件路径)中没有文件扩展名

vbscript - 为什么我无法连接到我的 access 数据库

windows - 是否可以在不链接到 c 库且不使用 ExitProcess() 的情况下从汇编代码创建 Windows exe?

java - 从 Java 的 processbuilder 执行的 ffmpeg 在 Windows 7 下不返回

c# - 在 Windows 7 框(64 位)上从 vb.net 创建快捷方式