Home > Enterprise >  How to trigger environment reload using C# .NET?
How to trigger environment reload using C# .NET?

Time:01-09

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.

CodePudding user response:

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";

CodePudding user response:

Changing the PATH variable in Windows should not require a reboot. A simple solution is to add the directory containing your executable and any folder it uses to your PATH variable. For example:

System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86).ToString() "" ather for Program Files (x86) on " System.Environment.OSVersion.Name

  • Related