环境变量在c# [英] environmental variable in a c#

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

问题描述

我使用以下命令在ac#Console应用程序中为环境变量设置一个值。

I am using the following command to set a value to the environmental variable in a c# Console application.

 System.Environment.SetEnvironmentVariable(envvar, result,EnvironmentVariableTarget.Process);

在命令窗口中运行应用程序后,当我尝试回显该变量时,值。
我必须在批处理文件中使用这个应用程序。
我想要的功能,如SET命令。请帮助..

After running the application in the command window, when I try to echo that variable ,I cannot see the value. I have to use this application in a batch file. I want the functionality like SET command. Please help..

编辑:
我尝试使用System.Environment.SetEnvironmentVariable(envvar,result,EnvironmentVariableTarget.user)为了传播更改,我尝试了在Env VAr中传播更改。但我不能在同一命令窗口回显变量。

I tried using System.Environment.SetEnvironmentVariable(envvar,result,EnvironmentVariableTarget.user) and to propagate the change I tried this Propagating Change in Env VAr. But I cant echo the variable in same command window.

让我重写一个问题:
我想在c#中为Env Var设置一个值。我必须能够在同一命令窗口中使用该变量(即我不应该打开一个新的cmd窗口来看到更改)。我们使用SET命令,我们可以立即使用该变量.. rt?我想要这样的功能。 Plzz帮助

Let me rephrase the question: I want to set a value to a Env Var in c#. I must be able to use that variable in same command window (ie i should not open a new cmd window to see the change). We use SET command and we can use that variable immediately .. rt ? I want such functionality. Plzz help

推荐答案

当您使用EnvironmentVariableTarget.Process时,变量集只会在当前进程中可见, :

When you use EnvironmentVariableTarget.Process the variable set will only visible in the current process as you can see in this sample:

System.Environment.SetEnvironmentVariable("myVar", "myValue", EnvironmentVariableTarget.Process);
string s = System.Environment.GetEnvironmentVariable("myVar",EnvironmentVariableTarget.Process);

上面myVar将显示s =myValue,但在命令窗口中不可见。

Above myVar will show s = "myValue" but not visible in command window.

如果你想设置在命令窗口可见的值,那么你需要使用EnvironmentVariableTarget.User:

If you want to set the value visible at command windows then you need to use EnvironmentVariableTarget.User:

System.Environment.SetEnvironmentVariable("myVar", "myValue", EnvironmentVariableTarget.User);

这样设置myVar = myValue将被存储,然后你可以在命令窗口看到。

This way the setting myVar=myValue will be stored and then you can see on command windows.

详细示例位于这里

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

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