2011-06-15 35 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"))