This is an old question, but still ranks high in Google results.
The link in the accepted answer is now broken.
However, you can find a duplicate question (asked later) that still has exact answers here: GetEnvironmentVariable () and SetEnvironmentVariable () for the PATH variable
I posed this question as a duplicate, but until it is closed, here is the following code that worked for me:
string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; //get non-expanded PATH environment variable string oldPath = (string)Registry.LocalMachine.CreateSubKey(keyName).GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames); //set the path as an an expandable string Registry.LocalMachine.CreateSubKey(keyName).SetValue("Path", oldPath + ";%MYDIR%", RegistryValueKind.ExpandString);
I replaced% MYDIR% with the application path.
In addition, you will need to perform your own action to place this code and put the code in a commit function.
source share