473,574 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PATH environment variable

Hi all, I am trying to append a certain string to the
PATH environment variable programmaticall y. I am able to
read what is in the variable using the System.Environm ent
method GetEnvironmentV ariable("path") . However, I don't
know yet how to append strings to the path variable. Any
help is appreciated. Thanks a lot.
Nov 16 '05 #1
4 39068
There is no managed way to set the environment variable strings.
Rather, you will have to call the SetEnvironmentV ariable function through
the P/Invoke layer.

If you are using .NET 2.0, then you can use the static
SetEnvironmentV ariable method on the Environment class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<an*******@disc ussions.microso ft.com> wrote in message
news:32******** *************** *****@phx.gbl.. .
Hi all, I am trying to append a certain string to the
PATH environment variable programmaticall y. I am able to
read what is in the variable using the System.Environm ent
method GetEnvironmentV ariable("path") . However, I don't
know yet how to append strings to the path variable. Any
help is appreciated. Thanks a lot.

Nov 16 '05 #2
an*******@discu ssions.microsof t.com wrote:

Hi all, I am trying to append a certain string to the
PATH environment variable programmaticall y. I am able to
read what is in the variable using the System.Environm ent
method GetEnvironmentV ariable("path") . However, I don't
know yet how to append strings to the path variable. Any
help is appreciated. Thanks a lot.


Many ways.

One is to do the following:

string path = @"%PATH%\subfol der\yourapplica tion.exe";
string expanded = System.Environm ent.ExpandEnvir onmentVairables (path);

Other way:

string expanded = System.Environm ent.GetEnvironm entVariable("pa th") +
@"\subfolder\yo urapplication.e xe";
Nov 16 '05 #3
Thanks for your reply Nicholas. Can you explain how we
can call the SetEnvironmentV ariable through the P/Invoke
layer? Thanks a lot in advance.
-----Original Message-----
There is no managed way to set the environment variable strings.Rather, you will have to call the SetEnvironmentV ariable function throughthe P/Invoke layer.

If you are using .NET 2.0, then you can use the staticSetEnvironment Variable method on the Environment class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<an*******@dis cussions.micros oft.com> wrote in message
news:32******* *************** ******@phx.gbl. ..
Hi all, I am trying to append a certain string to the
PATH environment variable programmaticall y. I am able to read what is in the variable using the System.Environm ent method GetEnvironmentV ariable("path") . However, I don't
know yet how to append strings to the path variable. Any help is appreciated. Thanks a lot.

.

Nov 16 '05 #4
If you want to change the parent (system) path environment, here is how I did
it:

// Import library used to broadcast a system message that the environment
changed.
[DllImport("user 32.dll", SetLastError=tr ue, CharSet=CharSet .Auto)]
static extern IntPtr SendMessageTime out(IntPtr hWnd, uint Msg,
UIntPtr wParam, string lParam, SendMessageTime outFlags fuFlags,
uint uTimeout, out UIntPtr lpdwResult);

public static string RegKey = @"System\Curren tControlSet\Con trol\Session
Manager\Environ ment";

public static string NewPathForFolde r = "d:\bin";
public enum SendMessageTime outFlags:uint
{
SMTO_NORMAL= 0x0000
, SMTO_BLOCK = 0x0001
, SMTO_ABORTIFHUN G = 0x0002
, SMTO_NOTIMEOUTI FNOTHUNG = 0x0008
}

private static void SetEnvironmentP ath()
{
// Update registry with environment variable path
RegistryKey key;
key = Registry.LocalM achine.OpenSubK ey(RegKey,true) ;
string path = (String)key.Get Value("Path");
string TestPath = NewPathForFolde r;
int a = path.IndexOf(Te stPath,1);
if (a == -1)
{
path += ";" + NewFolderForPat h;
key.SetValue("P ath", path);

//Notify all windows that User Environment variables are changed
IntPtr HWND_BROADCAST = (IntPtr) 0xffff;
const UInt32 WM_SETTINGCHANG E = 0x001A;
UIntPtr result;
IntPtr settingResult = SendMessageTime out(HWND_BROADC AST,
WM_SETTINGCHANG E,
(UIntPtr)0,
"Environmen t",
SendMessageTime outFlags.SMTO_N ORMAL,
10000,
out result);
}
}


"an*******@disc ussions.microso ft.com" wrote:
Hi all, I am trying to append a certain string to the
PATH environment variable programmaticall y. I am able to
read what is in the variable using the System.Environm ent
method GetEnvironmentV ariable("path") . However, I don't
know yet how to append strings to the path variable. Any
help is appreciated. Thanks a lot.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2235
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 = null; rkey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",true); string p =...
2
4387
by: shaun | last post by:
Is there anyway to access a system environment variable from within an xml document? Two applications: 1) I'd like to make an XBase declared path equal to an environment variable declared path. 2) In performing an xslt I'd like to insert the username and the OS architecture in the resulting document.
1
9679
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 DLL.) This all works great when the user's PATH variable is setup correctly. Unfortunately, there are times when the user's PATH is too full or...
1
2451
by: Bonj | last post by:
Hi My application installs a front-end GUI, which runs code when buttons are clicked. It also installs a command-line utility, that is a console application. They are both installed to the program's application directory, along with the DLL that contains the generic code that they both call. My question is, is there any way of modifying...
1
1113
by: beaker | last post by:
Hi, I want to be able to add to the PATH environment variable in one of my programs. Is there a way to do this? I know how to check the path using Environment.GetEnvironmentVariable("path"), just can't work out how to add to it! Thanks, Gary
10
3059
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python:2.4 Based on a search of other posts in this group, it appears as though os.environ is one way to obtain the PATH environment variable. My questions: 1) is it correct that os.environ contains the PATH environment variable? 2) are there other ways to obtain the PATH environment variable? if so,
0
1146
by: Chris Ashley | last post by:
Does the ASP.Net worker process not look in the System PATH environment variable for DLLs (unmanaged, non-COM)? If I place all the DLLs my app needs in a directory in the System PATH I get 'The specified module could not be found' in IIS (strangely it works in the built in VS web server). If I move all the DLLs to the System32 directory it...
1
1895
by: chandan | last post by:
I want to set the PATH environment variable to server machine through my application. I am using ASP.net 2.0 and language C#. Thanks in advance! Chandan
4
7503
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment variable is not in the os.environ dictionary. I know that cron runs in a subshell that does not have all of the normally set environment variables. ...
0
7726
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8052
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7815
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6452
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3740
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2243
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 we have to send another system
1
1339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.