2011-06-15 93 views

回答

13

可以使用WScript.Shell对象的ExpandEnvironmentStrings方法来检索环境变量。下面的代码将PATH环境变量的值分配给VAR mypath中:

set foo = createobject("WScript.Shell") 
myPath = foo.ExpandEnvironmentStrings("%PATH%") 

More info on the Shell object as MSDN

编辑:不得不改变到的壳对象被分配的变量。

+0

良好,即工作。谢谢! – MikeWyatt 2011-06-15 20:29:14

+0

非常好,很明显+1!但是权限呢?它是否需要任何提升的IUSR帐户权限? – 2011-06-16 08:09:35

2

以下为我工作的基础上,this article

Set objWSH = CreateObject("WScript.Shell") 
'This actually returns all the User Variables, and you either loop through all, or simply print what you want 
Set objUserVariables = objWSH.Environment("USER") 
MsgBox(objUserVariables("TEMP")) 

'This returns all the System Variables, and you either loop through all, or simply print what you want 
Set objSystemVariables = objWSH.Environment("SYSTEM") 
MsgBox(objSystemVariables("PATH"))