Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variable Path using C# [duplicate]

Tags:

c#

mysql

I am trying to set path environment variable for 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...

like image 862
user3313131 Avatar asked Feb 28 '14 11:02

user3313131


People also ask

How to set path and other environment variables in Windows 10?

Set PATH and other environment variables in Windows 10 1 Windows XP - Right-click My Computer, and then click Properties → Advanced → Environment variables → Choose New, Edit or... 2 Windows 7 - Click on Start → Computer → Properties → Advanced System Settings → Environment variables → Choose New, Edit... More ...

How do I create and edit environment variables?

Note: You can edit other environment variables by highlighting the variable in the "System variables" section and clicking Edit. If you need to create a new environment variable, click New and enter the Variable name and Variable value. To view and set the path in the Windows command line, use the path command.

How do I change the path and variables in Windows Vista?

Setting the path and variables in Windows Vista and Windows 7. From the Desktop, right-click the Computer icon and select Properties. Click the Advanced System Settings link in the left column. In the System Properties window, click on the Advanced tab, then click the Environment Variables button near the bottom of that tab.

How to add directory to system path environment variable in Linux?

Add directory to system path environment variable: Open administrator command prompt. Run the below command. pathman /as directoryPath. Remove path from system path environment variable: Run the below command from elevated command prompt. pathman /rs directoryPath.


2 Answers

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.

var name = "PATH";
var scope = EnvironmentVariableTarget.Machine; // or User
var oldValue = Environment.GetEnvironmentVariable(name, scope);
var newValue  = oldValue + @";C:\Program Files\MySQL\MySQL Server 5.1\bin\\";
Environment.SetEnvironmentVariable(name, newValue, scope);
like image 188
Steven Liekens Avatar answered Oct 07 '22 13:10

Steven Liekens


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

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

like image 27
Sunny Sharma Avatar answered Oct 07 '22 11:10

Sunny Sharma