从C#应用程序批量访问设置的变量 [英] Access variable set in batch from C# application

查看:90
本文介绍了从C#应用程序批量访问设置的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件setEnv.bat.

@echo off
set input=C:\Program Files\Java\jdk1.8.0_131
SET MY_VAR=%input%

我想从C#应用程序运行此批处理文件,并想从c#应用程序访问新设置的MY_VAR值.

I want to run this batch file from C# application and want to access the newly set value of MY_VAR from c# application.

C#:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName= "D:\\Check\\SetJavaHome.bat";
proc.StartInfo.WorkingDirectory = 
System.Environment.CurrentDirectory;
proc.Start();

字符串myVar = Environment.GetEnvironmentVariable("MY_VAR");

string myVar = Environment.GetEnvironmentVariable("MY_VAR");

有人可以帮助我按预期工作吗?

Can someone help me in getting this working as expected?

谢谢.

推荐答案

使用示例代码查看以下答案: https://stackoverflow.com/a/51189308/9630273

Check out this answer with the sample code: https://stackoverflow.com/a/51189308/9630273

不可能直接从另一个进程获取Environment变量,但是一个简单的解决方法可能是:

Getting the Environment variables directly from another process is not possible, but a simple workaround can be like:

创建一个虚拟蝙蝠文件(env.bat),该文件将执行所需的bat并回显环境变量. 在C#的流程执行中获取此env.bat的输出.

Create a dummy bat file (env.bat) that will execute the required bat and echo the environment variable. Get the output of this env.bat inside the process execution of C#.

这篇关于从C#应用程序批量访问设置的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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