简体   繁体   English

VBScript-修改Windows环境变量

[英]VBScript - Modify Windows Env Variables

I currently use a batch file to override some environment variables to make a game portable. 我目前使用批处理文件覆盖一些环境变量,以使游戏可移植。

Mainly the below: 主要如下:

Set USERPROFILE = %CD%

This only stays in effect through that session or through a program loaded from the batch. 这仅在该会话或从批处理加载的程序中有效。 So when I load the game through it with: 因此,当我通过以下方式加载游戏时:

Start "" "FL.exe"

The game treats the My Documents, My Pictures and AppData folders as being within my game folder. 游戏将“我的文档”,“我的图片”和“ AppData”文件夹视为在我的游戏文件夹中。

Can this functionality be replicated within VBScript at all? 可以完全在VBScript中复制此功能吗?

Yes, through the WshShell.Environment object. 是的,通过WshShell.Environment对象。 You can query, add, and remove environmental variables at the system, user, and process level. 您可以在系统,用户和过程级别查询,添加和删除环境变量。

For example: 例如:

Set objShell = CreateObject("WScript.Shell")
Set objProcessVars = objShell.Environment("process")

' Query existing environmental variables...
For Each objVar In objProcessVars
    WScript.Echo objVar
Next

' Add/Override a process-level variable...
objProcessVars("USERPROFILE") = "C:\Test"

' Spawned processes inherit your process-level variables...
objShell.Run "C:\AnotherScript.vbs"

' When script terminates, changes are lost.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM