1

I'm trying to add a value to the Path environment variable, but I can't seem to get it to work. I've went through a couple of similar questions and I'm pretty sure I have exactly the same code, but still it won't add the variable or I can't see it. I've checked both the administrator and local user account for changes. I've checked both during and after the running (debugging) of the application. I've also close VS2013 completely and checked.

Here's the code I'm using

string path = @"C:\Users\bono\Documents\Visual Studio 2013\Projects\3D-Scanner\AddEnviromentToPath\bin\Debug\AddEnviromentToPath.exe";
ProcessStartInfo process_start_info = new ProcessStartInfo();
process_start_info.FileName = path;
process_start_info.Verb = "runas";
process_start_info.WindowStyle = ProcessWindowStyle.Normal;
process_start_info.UseShellExecute = true;
process_start_info.Arguments = PATH_TO_PCL;

Process.Start(process_start_info); //Process that handles the adding of the value

AddEnviromentToPath program:

class Program {
    static void Main(string[] args) {
        //Just to make sure we're adding both
        AddToEnvironmentPath(args[0], EnvironmentVariableTarget.User);
        AddToEnvironmentPath(args[0], EnvironmentVariableTarget.Machine);
    }

    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);
    }
}

Note that I'm running VS2013 and the main application as a standard user. When the AddEnviromentToPath program runs I get an admin verification panel. I log in here with an admin account.

Edit:

Other people seem to get it working with basically the same code:
How do I get and set Environment variables in C#?
Environment is not being set in windows using c#. Where am I going wrong?

Community
  • 1
  • 1
Bono
  • 4,757
  • 6
  • 48
  • 77
  • You haven't made it at all clear what you intend to do. Do you wish to change the environment for the executing process, or do you want to change the default environment for new processes? – David Heffernan Aug 07 '15 at 08:09
  • @DavidHeffernan I didn't think that would matter if I used Machine as target. Documentation said it would set a permanent value. So I basically want to add it once to the Path variable and be done with it. The same what happens when you install some application (like MySQL or whatever) – Bono Aug 07 '15 at 11:38
  • Why don't you ask a question that states clearly what you are trying to do. It's still not clear. Your question doesn't address the key issue which is what my first comment describes. – David Heffernan Aug 07 '15 at 11:39
  • Alright well thanks for the link to that question. I'll try it out when I'm back on Monday. – Bono Aug 07 '15 at 11:46
  • @DavidHeffernan Can I ask another question, because I just really don't understand this enviroment variable business it seems. In the code I've given above; does the value get added but only for as long as the process runs? That would be enough for me, I just want to program to be able to concatenate a value to the `Path` environment variable. I need to have certain library files available from there. I have a different program which has been compiled etc. but requires these files and looks in the `Path` variable for them. Would my code be sufficient, or would I still need yours (in the dupe)? – Bono Aug 17 '15 at 04:44
  • @DavidHeffernan It's just hard for me to wrap my head around this, because it seems like there is no way to really check if the environment variable has been changed (or if there is I'd be glad to know). I did try your code in the dupe btw, and again I couldn't find that the `Path` variable was edited. Does that code also make the `Path` var changed for as long as the process runs and afterwards it changes back? Sorry about the bunch of questions. I hope you can answer them if you have the time. – Bono Aug 17 '15 at 04:48
  • Don't change the path at all. Use SetDllDirectory or AddDllDirectory. – David Heffernan Aug 17 '15 at 06:17
  • @DavidHeffernan The program I was referring to is a C++ program so I can't set it using that. – Bono Aug 17 '15 at 06:42
  • Yes you can. This is very confused now. First it's C#. Now it's C++. First you want to modify path, now you want to influence dll search order. – David Heffernan Aug 17 '15 at 06:46
  • @DavidHeffernan Alright: I have a C# program that calls a C++ program. That C++ program is looking for dll's with help from the `Path` environment variable. That's why I want to add the values to the `Path` variable. If that is only for as long as the C# program (and therefor the C++ process) is open that's fine with me. – Bono Aug 17 '15 at 06:48
  • Don't modify path to influence dll search order. Use the function I listed before. If you don't think I know what I'm talking about, don't respond to my comments. – David Heffernan Aug 17 '15 at 06:49
  • @DavidHeffernan I actually do think you know what you're talking about, that's why I'm asking you. I was just trying to make sure you knew I was referring to the C++ program. I thought that you were talking about a C# function and not a C++ function, sorry about that (I looked it up and thought it was a C# function). Thanks for all the help, sorry to be pushing your buttons. I do appreciate the effort. – Bono Aug 17 '15 at 06:53

2 Answers2

1

Assuming that Environment.SetEnvironmentVariable calls the Win32 SetEnvironmentVariable function behind the scenes, this note is probably applicable:

Sets the contents of the specified environment variable for the current process
...
This function has no effect on the system environment variables or the environment variables of other processes.

If you want to change a global environment variable and have existing processes notice it, you need to:

  • Save it to the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
  • Notify existing processes of the change with the WM_SETTINGCHANGE message.

See the MSDN Environment Variables documentation for more information.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Alright, but as a question; How come other people seem to be able to get it working with basically the same code? :/ Examples: [1](http://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c) [2](http://stackoverflow.com/questions/27286713/environment-is-not-being-set-in-windows-using-c-where-am-i-going-wrong/27287816#comment51659421_27287816) – Bono Aug 07 '15 at 06:45
  • I don't know; I don't actually know anything about .net and this answer may well be wrong :) – Jonathan Potter Aug 07 '15 at 06:59
0

ProcessStartInfo to the rescue!

You need to check this documentation out:

https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.environmentvariables%28v=vs.110%29.aspx

The key text which addresses your concerns:

Although you cannot set the EnvironmentVariables property, you can modify the StringDictionary returned by the property.

code4life
  • 15,655
  • 7
  • 50
  • 82