2012-01-02 148 views
0

我正在制作一个安装程序,它将100%取决于系统变量,如%TEMP%或%Path%,用户首先双击windows.bat文件。如何使用BATCH或VBS使永久系统变量?

但是,如何在Windows XP,Vista,7,8使用VBS或BATCH设置永久系统变量?

我尝试了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 
+0

试着让你的安装程序仅0%取决于系统变量。 – 2012-01-02 21:12:36

+1

@UweKeim:0%安装程序,但应用程序是100%需要一些定制的系统变量。特别是我的第三方库。 – YumYumYum 2012-01-02 21:14:02

回答

3

您可以创建在注册表中HKEY_LOCAL_MACHINE \ SYSTEM的REG_SZ值\ CURRENTCONTROLSET \控制\会话管理\环境。值的名称指定环境变量的名称,该值指定环境变量的值。

您也可以修改现有的值。

为了修改注册表,您可以使用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" 
+1

1)http://ss64.com/nt/reg.html 2)REG添加“HKLM \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment”/ v MyApplicationWillUsetThis/d“C:\ WhatEverPathToIncludeHereQuestionMark”? – YumYumYum 2012-01-02 21:20:23

+1

是的,这是另一种方式。 – 2012-01-02 21:22:21

+0

我做了上面我的编辑。但它会询问Y或N.如何在命令中明确应用“是的,总是”? – YumYumYum 2012-01-02 21:22:58

2

如何从VBScript

Set WshShell = CreateObject("Wscript.Shell") 

' set a permanent environment variable %MyVarName% for all users 
WshShell.Environment.item("MyVarName") = "MyVarValue" 

设置环境变量如果上面的设置是不是永久性的,试试这个。这将为所有用户设置永久环境变量。您可以使用Environment集合尝试System,User,Volatile和Process类别。

With WSHShell.Environment("SYSTEM") 
    .item("MyVarName") = "MyVarValue" 
End With 

您还可以使用ExpandEnvironmentStrings阅读环境变量或与长命令行中输入字符串值替换它们。

sValue = WshShell.ExpandEnvironmentStrings("%MyVarName%") 

与注册表的工作尝试

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