vb.net 使用visual basic 2010设置环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18624213/
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-09-17 14:55:58  来源:igfitidea点击:

Setting an environment variable with visual basic 2010

vb.netvisual-studio-2010

提问by user2333346

Im trying to set an environment variable in Visual Basic 2010 as follows:

我试图在 Visual Basic 2010 中设置一个环境变量,如下所示:

Module Module1
Sub Main()
        Environment.SetEnvironmentVariable("Variable", "Value")

 End Sub
End Module

But even though the code runs,

但即使代码运行,

When i go to Environment Variables>System Variables Nothing is there

当我去环境变量>系统变量时什么都没有

Can someone please tell me whats wrong?

有人可以告诉我有什么问题吗?

回答by Jimmy

By default, the environment variable is only set for the current process (and inherited in any child processes that spawn from it). You're probably looking for the overloadwhich lets you specify an EnvironmentVariableTarget(either Machine or User for your case).

默认情况下,环境变量仅为当前进程设置(并在从它产生的任何子进程中继承)。您可能正在寻找可以让您指定EnvironmentVariableTarget(您的情况下的机器或用户)的重载

回答by Avijit Karmakar

To set the environment variable for the the current process:

为当前进程设置环境变量:

Environment.SetEnvironmentVariable(String EnvironmentVariableName, String EnvironmentVariableValue, EnvironmentVariableTarget.Process)

To set the environment variable for the the current user:

为当前用户设置环境变量:

Environment.SetEnvironmentVariable(String EnvironmentVariableName, String EnvironmentVariableValue, EnvironmentVariableTarget.User)

To set the environment variable for the the local machine:

为本地机器设置环境变量:

Environment.SetEnvironmentVariable(String EnvironmentVariableName, String EnvironmentVariableValue, EnvironmentVariableTarget.Machine)