G
Guest
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\CurrentControlSet\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.
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\CurrentControlSet\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.