469,610 Members | 2,112 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

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

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.
Nov 16 '05 #1
6 6249
> 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.


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.

Nov 16 '05 #2
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?

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
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.


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.


Nov 16 '05 #3
> 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...

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
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.


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.



Nov 16 '05 #4
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.

"Garfield" <ga******@nospam.com> wrote in message
news:et****************@tk2msftngp13.phx.gbl...
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?

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
> 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.

>
> 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.
>
>



Nov 16 '05 #5
Hah ;)
Look here:
http://pinvoke.net/default.aspx/user...MessageTimeout

There is an exact example for what I said before

"Igor" <no@way.com> wrote in message
news:uZ**************@TK2MSFTNGP12.phx.gbl...
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.

"Garfield" <ga******@nospam.com> wrote in message
news:et****************@tk2msftngp13.phx.gbl...
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?

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
> 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.
>
> 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.
>
>



Nov 16 '05 #6
Thanks Igor! I'll have a look at the link you provided.

"Igor" <no@way.com> wrote in message
news:ux**************@tk2msftngp13.phx.gbl...
Hah ;)
Look here:
http://pinvoke.net/default.aspx/user...MessageTimeout

There is an exact example for what I said before

"Igor" <no@way.com> wrote in message
news:uZ**************@TK2MSFTNGP12.phx.gbl...
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.

"Garfield" <ga******@nospam.com> wrote in message
news:et****************@tk2msftngp13.phx.gbl...
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?

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
> 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.
>
> 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.
>
>



Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jeff Wagner | last post: by
1 post views Thread by Viktor Popov | last post: by
6 posts views Thread by ajikoe | last post: by
TMS
3 posts views Thread by TMS | last post: by
5 posts views Thread by =?Utf-8?B?SmVmZiBCZWVt?= | last post: by
1 post views Thread by harvey | last post: by
1 post views Thread by fts2012 | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.