Problem Using C# to Append Paths to Path Env. Var.

G

Garfield

We have written a small function to append a couple of paths to the path
environmental variable. The code is:

string PathValue = "";
string sAdd = "";

sAdd = ";" + <path1> + "Bin;" + <path2>;

PathValue = System.Environment.GetEnvironmentVariable("Path");
PathValue += sAdd;

Microsoft.Win32.RegistryKey IniKey;
IniKey =
Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\
\Control\\Session Manager\\Environment", true);
IniKey.SetValue("Path", PathValue);
IniKey.Close();

This seems to work fine. However, the path environment variable is not
updated dynamically. It appears we have to reboot the computer for this
change to take effect. The problem is that we need the changes to take
effect immediately.

Using C#, is there any way to append a path or two to the path environment
variable so that it takes effect immediately?

Thanks in advance.
 
B

Bonj

However, the path environment variable is not
updated dynamically. It appears we have to reboot the computer for this
change to take effect. The problem is that we need the changes to take
effect immediately.

I used to think this, and then I discovered that you only actually have to
restart the application.
They presumably load into their memory space the environment variables on
process start or something.
 
G

Garfield

Hi, thanks for the info. However, the application we're writing cannot be
restarted in the middle of its process. Basically, we have an application
that performs a number of steps in sequence, including updating the path
env. var. Then, a subsequent step needs that path env. var. to be updated
so that it can do its thing. Finally, each step needs to completed before
the application can be stopped (and restarted).

So, is there a way to set the path env. var. and the updates available right
away without restarting the application or rebooting the computer?
 
G

Guest

Hi, thanks for the info. However, the application we're writing cannot be
restarted in the middle of its process.

Then perhaps you could look into setting the environment variables to the
correct values before the program starts?
Basically, we have an application
that performs a number of steps in sequence, including updating the path
env. var. Then, a subsequent step needs that path env. var. to be updated
so that it can do its thing. Finally, each step needs to completed before
the application can be stopped (and restarted).

I'd say that the only reason I can think of why changes to environment
variables would *need* to be reflected for a program's subsequent operations
and that it *can't possibly* restart, is to preserve legacy code.
Sometimes it does feel like you're taking a step backwards in order to take
a step forwards, but believe me, environment variables are not the place to
store dynamic data.
So, is there a way to set the path env. var. and the updates available right
away without restarting the application or rebooting the computer?

No..! Unfortunately for you, there isn't...
 
I

Igor

Hi Garfield,

I'm more C++ developer then C# so I can just give the following advice:
try to broadcast WM_SETTINGCHANGE message(using P/Invoke, I suppose) after
changing the PATH var.
This is how I did using WinAPI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top