2
string path = System.Environment.GetEnvironmentVariable("Path");
Console.WriteLine(path);
if (!path.Contains("C:\ccstg"))
{
    if (!path.EndsWith(";"))
        path = path + ';';
    var v=@"C:\ccstg;";
    path = path + v;
    Environment.SetEnvironmentVariable("Path",path);
    Console.WriteLine(path);
    Console.WriteLine("Path Set");
    Console.ReadKey();
}

I am trying to set path environment variable using c#, I am able to get "Path" but while setting it is not being set. It doesn't show any error either. I have tried running it as administrator also , no help.

Does anybody what am I missing here ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
mkkhedawat
  • 1,687
  • 2
  • 17
  • 34

2 Answers2

7

Firstly, you need to be a little more careful with your string literals, the code you posted won't compile because "\c" is not a valid string literal escape sequence. To fix:

        string newPathComponent = @"C:\ccstg";
        if (!path.Contains(newPathComponent))
        {
            if (!path.EndsWith(";"))
                path = path + ';';
            path = path + newPathComponent;
            Environment.SetEnvironmentVariable("Path", path);

Now, this code works and sets the path for the duration of the process. If you want to set the path permanently, you need to use Environment.SetEnvironmentVariable Method (String, String, EnvironmentVariableTarget), for instance:

            var target = EnvironmentVariableTarget.User; // Or EnvironmentVariableTarget.Machine
            Environment.SetEnvironmentVariable("Path", path, target);

More here.

However, if you do that, you have to be careful to add your path component only to the path associated with that EnvironmentVariableTarget. That's because the %PATH% environment variable is actually combined from several sources. If you aren't careful, you may copy the combined path into just the EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User source -- which you do not want to do.

Thus:

    static void AddToEnvironmentPath(string pathComponent, EnvironmentVariableTarget target)
    {
        string targetPath = System.Environment.GetEnvironmentVariable("Path", target) ?? string.Empty;
        if (!string.IsNullOrEmpty(targetPath) && !targetPath.EndsWith(";"))
            targetPath = targetPath + ';';
        targetPath = targetPath + pathComponent;
        Environment.SetEnvironmentVariable("Path", targetPath, target);
    }

Finally, if you are running inside the Visual Studio Hosting Process for debugging, I have observed that if you use Environment.SetEnvironmentVariable("Path",path, EnvironmentVariableTarget.User), changes to the permanent environment will not be picked up until you exit and restart visual studio. Something weird to do with the visual studio hosting process, I reckon. To handle this odd scenario you might want to do both:

AddToEnvironmentPath(@"C:\ccstg", EnvironmentVariableTarget.User)
AddToEnvironmentPath(@"C:\ccstg", EnvironmentVariableTarget.Process)
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Man, by far the best and most complete answer I've seen on this subject. Ridiculous how someone with "Use this method" got 150 upvotes and this answer is left here to die. – Bono Aug 07 '15 at 05:47
1

SetEnvironmentVariable sets the variable for the current process. Your process has its own environment. When you set an environment variable in your program, it only affects your program's environment.

If you want to affect the user's environment, that is, make the change so that it can be seen outside your program, then you have to call this overload. For example:

Environment.SetEnvironmentVariable("Path", path, EnvironmentVariableTarget.User);

See EnvironmentVariableTarget enumeration for more details.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351