473,466 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6398
> 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 words, where and in what file on a Win32 box or...
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 gives me "C:\\" instead of my homepath. I have...
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 isn't written the path to the file(the text field of...
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 say I have a web application appA. Locally, I set...
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: # prmodule.c static PyObject *pr_isprime(PyObject *self,...
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 the maze based on a dictionary that has each path...
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 drives as it's path of choice. The reason for...
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 with a foreach/glob() at the moment but have others...
1
by: fts2012 | last post by:
follow the dive into python ----------------------------------------------------------------- ----------------------------------------------------------------- I append the filepath of <<dive into...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.