469,600 Members | 2,189 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

modify the path environment variable

I have a need within an application to modify the path environment variable,
as I need to find specific directories and remove them. I use the following
code to do this:

RegistryKey rkey = null;
rkey =
Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentC ontrolSet\Control\Session
Manager\Environment",true);
string p = (String)rkey.GetValue("Path");
string[] pp = p.Split(';');
string outPath = "";
foreach(string t in pp) {
string t2 = t.ToUpper();
if (t2.IndexOf("MQCLIENT") < 0) {
outPath += t + ";";
}
}
rkey.SetValue("Path",p);
rkey.Close();

I break the path apart into an array using the semi-colon delimiter. If the
string I'm looking for isn't in this folder, it gets added to the out path
string. This works about 95% of the time. Sometimes it just seems like the
app isn't able to modify this value. It doesn't generate any exceptions, it
just doesn't change the path as it should. It happens on XP machines as well
as Win2k machines. The folder I want to remove has been in varying spots in
the path. I haven't been able to discover anything consistent about the
machines where this fails. I checked permissions on this registry section,
even changing it so Everyone had full access, and it made no difference.
There has to be something in my code that I'm missing, but what that is, I
don't know.
Jul 21 '05 #1
1 1924
I assume this is not the actual code because it should not work at all. You
are assigning the registry value of the Path to the variable "p" and then
updating the registry with the same value.

string p = (String)rkey.GetValue("Path");
..
..
..
rkey.SetValue("Path",p);

There is also a problem with your foreach loop. If the value is not in the
path, you will end up adding it over and over again. You don't need to loop
at all, just look at the entire path and determine if the specific value is
anywhere in it and add it if necessary.
"jwpioneer" wrote:
I have a need within an application to modify the path environment variable,
as I need to find specific directories and remove them. I use the following
code to do this:

RegistryKey rkey = null;
rkey =
Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentC ontrolSet\Control\Session
Manager\Environment",true);
string p = (String)rkey.GetValue("Path");
string[] pp = p.Split(';');
string outPath = "";
foreach(string t in pp) {
string t2 = t.ToUpper();
if (t2.IndexOf("MQCLIENT") < 0) {
outPath += t + ";";
}
}
rkey.SetValue("Path",p);
rkey.Close();

I break the path apart into an array using the semi-colon delimiter. If the
string I'm looking for isn't in this folder, it gets added to the out path
string. This works about 95% of the time. Sometimes it just seems like the
app isn't able to modify this value. It doesn't generate any exceptions, it
just doesn't change the path as it should. It happens on XP machines as well
as Win2k machines. The folder I want to remove has been in varying spots in
the path. I haven't been able to discover anything consistent about the
machines where this fails. I checked permissions on this registry section,
even changing it so Everyone had full access, and it made no difference.
There has to be something in my code that I'm missing, but what that is, I
don't know.

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by wen | last post: by
1 post views Thread by Chris B | last post: by
4 posts views Thread by | last post: by
1 post views Thread by jwpioneer | last post: by
2 posts views Thread by Daniel Mark | last post: by
3 posts views Thread by HMS Surprise | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.