473,396 Members | 2,024 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,396 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 2207
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.