设置在JavaScript环境变量 [英] Setting an environment variable in javascript

查看:1225
本文介绍了设置在JavaScript环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何可以设置调用其他程序的JScript WSH文件中的环境变量?下面是减少测试用例:

How can I set an environment variable in WSH jscript file that calls another program? Here's the reduced test case:

envtest.js
----------
var oShell = WScript.CreateObject("WScript.Shell");
var oSysEnv = oShell.Environment("SYSTEM");
oSysEnv("TEST_ENV_VAR") = "TEST_VALUE";
oExec = oShell.Run("envtest.bat", 1, true);    

envtest.bat
-----------
set
pause

我希望看到TEST_ ENV _var在变量列表中,但它不存在。怎么了?

I expect to see TEST_ ENV _VAR in the list of variables, but it's not there. What's wrong?

编辑:

如果有人能产生一个工作code样品,我会庆祝,作为正确答案。 :)

If someone can produce a working code sample, I'll mark that as the correct answer. :)

推荐答案

问题是不是在你的code,但它是在这个过程中的执行。完整的系统变量被分配给被执行的处理。所以,你的孩子的过程也有过相同的变量。

The problem is not in your code, but it is in execution of the process. The complete system variables are assigned to the process which is executed. so, your child process also had the same set of variables.

您code-样品做工不错。它增加了变量系统环境。

Your code-sample works good. It adds the variable to the SYSTEM environment.

所以,你需要设置变量,不仅为你的系统也为你的过程。

So, you need to set the variable not only for your system but also for your process.

这里的code。

var oShell = WScript.CreateObject("WScript.Shell");
var oSysEnv = oShell.Environment("SYSTEM");
oSysEnv("TEST1") = "TEST_VALUE";
var oSysEnv = oShell.Environment("PROCESS");
oSysEnv("TEST1") = "TEST_VALUE";
oExec = oShell.Run("envtest.bat", 1, true);  

一旦你创建的系统变量。

Once you created the system variable.

这将分配当前进程新创建的变量。所以,当SET命令来执行你的孩子的过程能得到该变量。

It will assign the newly created variable for the current process. So, your child process can get that variable while the "SET" command executed.

对不起,我不好英语。

这篇关于设置在JavaScript环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆