如何在 C# 中获取和设置环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/185208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:03:06  来源:igfitidea点击:

How do I get and set Environment variables in C#?

c#.net.net-2.0environment-variables

提问by Mister Dev

How can I get Environnment variables and if something is missing, set the value?

如何获取环境变量,如果缺少某些内容,请设置值?

采纳答案by Patrick Desjardins

Use the System.Environmentclass.

使用System.Environment类。

The methods

方法

var value = System.Environment.GetEnvironmentVariable(variable [, Target])

and

System.Environment.SetEnvironmentVariable(variable, value [, Target])

will do the job for you.

会为你做这项工作。

The optional parameter Targetis an enum of type EnvironmentVariableTargetand it can be one of: Machine, Process, or User. If you omit it, the default target is the current process.

可选参数Target是类型的枚举EnvironmentVariableTarget,它可以是以下之一:MachineProcess,或User。如果省略它,则默认目标是当前进程。

回答by SpeedyNinja

This will work for an environment variable that is machine setting. For Users, just change to User instead.

这将适用于机器设置的环境变量。对于用户,只需改为用户即可。

String EnvironmentPath = System.Environment
                .GetEnvironmentVariable("Variable_Name", EnvironmentVariableTarget.Machine);

回答by Nathan Bedford

I ran into this while working on a .NET console app to read the PATH environment variable, and found that using System.Environment.GetEnvironmentVariable will expand the environment variables automatically.

我在使用 .NET 控制台应用程序读取 PATH 环境变量时遇到了这个问题,发现使用 System.Environment.GetEnvironmentVariable 会自动扩展环境变量。

I didn't want that to happen...that means folders in the path such as '%SystemRoot%\system32' were being re-written as 'C:\Windows\system32'. To get the un-expanded path, I had to use this:

我不希望这种情况发生……这意味着路径中的文件夹(例如“%SystemRoot%\system32”)被重写为“C:\Windows\system32”。为了获得未扩展的路径,我不得不使用这个:

string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\";
string existingPathFolderVariable = (string)Registry.LocalMachine.OpenSubKey(keyName).GetValue("PATH", "", RegistryValueOptions.DoNotExpandEnvironmentNames);

Worked like a charm for me.

对我来说就像一种魅力。

回答by Karthik Chintala

Environment.SetEnvironmentVariable("Variable name", value, EnvironmentVariableTarget.User);

回答by Tom Stickel

Get and Set

获取和设置

Get

得到

string getEnv = Environment.GetEnvironmentVariable("envVar");

Set

string setEnv = Environment.SetEnvironmentVariable("envvar", varEnv);

回答by Ajit

I could be able to update the environment variable by using the following

我可以使用以下方法更新环境变量

string EnvPath = System.Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine) ?? string.Empty;
if (!string.IsNullOrEmpty(EnvPath) && !EnvPath .EndsWith(";"))
    EnvPath = EnvPath + ';';
EnvPath = EnvPath + @"C:\Test";
Environment.SetEnvironmentVariable("PATH", EnvPath , EnvironmentVariableTarget.Machine);