简体   繁体   English

如何在 .NET Core 中设置全局环境变量(用户范围或系统范围)

[英]How to set a global environment variable in .NET Core (user-wide or system-wide)

In full .NET we can pass EnvironmentVariableTarget enum to Environment.SetEnvironmentVariable call:在完整的 .NET 中,我们可以将EnvironmentVariableTarget枚举传递给Environment.SetEnvironmentVariable调用:

public enum EnvironmentVariableTarget
{
    Process,
    User,
    Machine
}

With User or Machine options we can set a user-wide or system-wide variable which stays live after application end:使用UserMachine选项,我们可以设置一个用户范围或系统范围的变量,该变量在应用程序结束后仍然有效:

Environment.SetEnvironmentVariable("foo", "bar", EnvironmentVariableTarget.User);

... ...

> echo %foo%
bar

However, Environment.SetEnvironmentVariable doesn't accept the variable target in .NET Core.但是, Environment.SetEnvironmentVariable不接受 .NET Core 中的变量目标。 Even XML comment for this method says:甚至此方法的 XML 注释也说:

Creates, modifies, or deletes an environment variable stored in the current process .创建、修改或删除存储在当前进程中的环境变量。

How to set an user-wide or machine-wide environment variable in .NET Core-targeted app?如何在面向 .NET Core 的应用程序中设置用户范围或机器范围的环境变量? Only a crossplatform solution makes sense (at least Windows and Linux).只有跨平台解决方案才有意义(至少是 Windows 和 Linux)。

For .NET Core 1.x it only allows setting an environment variable in the User context.对于 .NET Core 1.x,它只允许在用户上下文中设置环境变量。 With the new .NET Core 2.0 preview it allows using the EnvironmentVariableTarget .使用新的 .NET Core 2.0 预览版,它允许使用EnvironmentVariableTarget However, according to this GitHub issue setting User or Machine environment variables doesn't work on non-windows platforms.但是,根据GitHub 问题,设置用户或机器环境变量在非 Windows 平台上不起作用。

Using other scopes (eg User, Machine) on non-Windows platforms) is not mapped in the implementation.在非 Windows 平台上使用其他范围(例如用户、机器)在实现中没有被映射。

Also described in the GitHub issue is to use a workaround to check if it is a non-windows platform to set an environment variable. GitHub issue中还描述了使用一种解决方法来检查是否是非windows平台设置环境变量。 For example:例如:

// omitting check for presence of bash binary
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) System.Diagnostics.Process.Start("/bin/bash", "-c export " + variable + "=" + value);

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

相关问题 如何获取系统范围的.NET Core运行时环境的基本路径? - How to get the base path of system-wide .NET Core runtime environment? 是.net系统范围内的垃圾收集器还是应用程序范围内的垃圾收集器? - Is the garbage collector in .net system-wide or application-wide? 系统范围的热键 - 如何以编程方式检测全局键盘快捷键? - system-wide hot keys — how to detect global keyboard shortcuts programmatically? .NET系统范围内的EventWaitHandle名称允许的字符 - .NET system-wide EventWaitHandle name allowed characters 应用程序如何与系统范围的文本选择挂钩? - How can an app hook into text selection system-wide? 与“系统级”媒体播放器互动 - Interact with “system-wide” media player 系统范围的全局列表变量是否存在 - Does a System Wide Global List variable exist 取消处理(取消注册)系统范围的“HotKey” - Unhandling (unregistering) a system-wide 'HotKey' 从DB处理队列时,系统范围互斥的替代方案? - alternatives to system-wide mutex when processing queue from DB? C#异步接收会导致系统范围的网络崩溃! - C# asynchronous receive causes system-wide network crash!
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM