472,353 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 6335
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jeff Wagner | last post by:
Is it possible to append a new Path to some file permanently? It seems like a sys.path.append('/my/new/path') statement is temporary. In other...
9
by: Amir Dekel | last post by:
Hi everyone, I have two problems: 1. How can I keep my changes in sys.path after closing the interpreter? 2. os.path.expanduser("~") always...
1
by: Viktor Popov | last post by:
Hi, I'm trying to upload a file and I do that with this code, but there is a thing which doesn't work. I can't understand how to check if it...
8
by: nick | last post by:
I have a problem and I've been using a cheezy work around and was wondering if anyone else out there has a better solution. The problem: Let's...
6
by: ajikoe | last post by:
Hello I tried to combine c++ and python together. So I follow from this website: http://kortis.to/radix/python_ext/ I have this code: #...
3
TMS
by: TMS | last post by:
Hey all: I'm working on this 'pac man' like game I've been writing in Tkinter. The temporary gif I'm using (I can't attach it, sorry) goes around...
5
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
Before you respond with "just use GetShortPathName" please read the rest. We have an application that places files on a file server using mapped...
1
by: harvey | last post by:
Can anyone tell me how PHP handles paths? Do all functions require fully qualified paths or can some use relative paths? I'm having a problem...
1
by: fts2012 | last post by:
follow the dive into python -----------------------------------------------------------------...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.