如何使用C#设置环境变量Path

时间:2022-04-02 11:20:19

I am trying to set path environment variable for MySql.

我正在尝试为MySql设置路径环境变量。

I don't get an error, but my code doesn't work.

我没有收到错误,但我的代码不起作用。

Code:

First:

string pathvar = @";C:\Program Files\MySQL\MySQL Server 5.1\bin\\";
System.Environment.SetEnvironmentVariable("PATH", pathvar);

Second:

string pathvar = System.Environment.GetEnvironmentVariable("PATH");
System.Environment.SetEnvironmentVariable("PATH", pathvar + @";C:\Program Files\MySQL\MySQL Server 5.1\bin\\");

Thank you for your help...

感谢您的帮助...

2 个解决方案

#1


8  

You are associating the environment variable with your program, but instead you want to associate it with your local machine in order to make it available to every program. Look at the overload that takes an EnvironmentVariableTarget.

您正在将环境变量与您的程序相关联,但您希望将其与本地计算机相关联,以使其可用于每个程序。查看带有EnvironmentVariableTarget的重载。

const string name   = "PATH";
string pathvar = System.Environment.GetEnvironmentVariable(name);
var value  = pathvar + @";C:\Program Files\MySQL\MySQL Server 5.1\bin\\";
var target = EnvironmentVariableTarget.Machine;
System.Environment.SetEnvironmentVariable(name, value, target);

#2


1  

Calling SetEnvironmentVariable has no effect on the system environment variables. Check this answer here:

调用SetEnvironmentVariable对系统环境变量没有影响。在这里查看这个答案:

https://*.com/a/19705691/1057667

#1


8  

You are associating the environment variable with your program, but instead you want to associate it with your local machine in order to make it available to every program. Look at the overload that takes an EnvironmentVariableTarget.

您正在将环境变量与您的程序相关联,但您希望将其与本地计算机相关联,以使其可用于每个程序。查看带有EnvironmentVariableTarget的重载。

const string name   = "PATH";
string pathvar = System.Environment.GetEnvironmentVariable(name);
var value  = pathvar + @";C:\Program Files\MySQL\MySQL Server 5.1\bin\\";
var target = EnvironmentVariableTarget.Machine;
System.Environment.SetEnvironmentVariable(name, value, target);

#2


1  

Calling SetEnvironmentVariable has no effect on the system environment variables. Check this answer here:

调用SetEnvironmentVariable对系统环境变量没有影响。在这里查看这个答案:

https://*.com/a/19705691/1057667