473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Similar topics

8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
10
by: wen | last post by:
on my system(win2k server, python 2.3.5), >>> import sys >>> print sys.path now, i wanna add "C:\Python23\Pmw\Pmw_1_2\lib" into sys.path, how? any help would be appreciated. with my...
1
by: Chris B | last post by:
I have an application that uses PInvoke to call into native DLLs. My DLLimport attribute only has the name of the DLL that I need to import (i.e. I leave it up to the OS to search the PATH for the...
4
by: | last post by:
Hi all, I am trying to append a certain string to the PATH environment variable programmatically. I am able to read what is in the variable using the System.Environment method...
6
by: Garfield | last post by:
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;" +...
1
by: jwpioneer | last post by:
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 = ...
2
by: Daniel Mark | last post by:
Hello all: I installed IPython on my XP machine today and find that my sys.path has been changed as follows:
3
by: HMS Surprise | last post by:
Is sys.path setup differnently in jython vs python? I have environment variables pythonpath and jythonpath set to include C:\python22 but the initial printout indicates it is being ignored. Also...
2
by: Blair | last post by:
Hello, I have a old enviromantal path in LIB file that, every time I compile gives a error message. It does not stop the compiler just gives a warning. This has become very annoying. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.