Modifying an environment variable

sde

Centurion
Joined
Aug 28, 2003
Messages
160
Location
us.ca.fullerton
I need to add to the PATH environment variable. This can be done in either C# or command line.

I know in the cl I can do: SET PATH=%PATH%;c:\mypath , however this seems to only be a temorary thing. When I look at environment variables from My Computer->Properties, the path does not show. It only shows when I echo the PATH in the command line. When I reboot the computer, it is gone.

So, does anyone know how to add to the PATH environment variable and make it stay there?
 
When you were setting the path variable via the command line you were setting it only for that instance of the command prompt. As a matter of fact, the path variable that you set won't appear in another instance of a command prompt either -- it is only for the instance you set it in. When you set an environment variable via "My Computer" or the registry, it will be permanent. If you just need to add something to your path and it is not application specific, "My Computer" is the easiest way to go.
 
I did need to add a system path. The purpose of writing this app is to automate the process of configuring 20 different things into a simple button click. Going to my computer would defeat the purpose.

I found which registry key the path was in and here is the code that will modify it.

Code:
private void setEnvPath(string strNewPath)
{
  RegistryKey key = Registry.LocalMachine;
  key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",true);

  key.SetValue("Path", strNewPath );
  key.Close();
}
 
Back
Top