Search code examples
c#.netwindowsenvironment

How to trigger environment reload using C# .NET?


I am trying to write a custom installer for my application. I am making this installer using C#. One of the features of this is adding my app's directory to PATH.

However, when I do this, I have to reboot my computer to be able to access the updated PATH variable. When using a setup scripting tool like Inno setup, I can set it to somehow reload the environment to access the updated PATH immediately without having to reboot. How can I replicate this behaviour using C#?

I am sorry if this is a duplicate question but I am unable to find anything on this.


Solution

  • It turns out that I was trying to modify the registry. You can access it immediately by using System.Environment

    var name = "PATH";
    var scope = EnvironmentVariableTarget.User; // or User
    var oldValue = Environment.GetEnvironmentVariable(name, scope);
    var newValue = oldValue + @";C:\Path\To\Add";