开发者

Enumerating Environment keys with VBScript?

I need to enumerate the keys in the WScript.Shell.Environment object. It's clear that if you already know the name of the key, you're home free with:

Set oShell = WScript.CreateObject("WScript.Shell")
Debug.WriteLine "PATH=" & oShell.Environment("PATH")

...but if you want to list the keys, it looks like you're out of luck! Is there a secret pas开发者_StackOverflow社区sageway somewhere?


The WshEnvironment object is a collection, so you can enumerate it using VBScript'sFor Each ... Next statement:

Set oShell = WScript.CreateObject("WScript.Shell")
Set oEnv = oShell.Environment

For Each strVar in oEnv
  WScript.Echo strVar
Next

The output contains both environment variable names and values, like this:

ComSpec=%SystemRoot%\system32\cmd.exe
NUMBER_OF_PROCESSORS=2
TEMP=%SystemRoot%\TEMP
TMP=%SystemRoot%\TEMP
windir=%SystemRoot%
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜