473,837 Members | 1,862 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 39109
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
2250
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 = (String)rkey.GetValue("Path"); string pp = p.Split(';');
2
4401
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
9755
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 whatever to have the correct search path for my application. What I was wondering is if there is...
1
2464
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 the environment variable permanently with the setup project, such that the user has got the...
1
1120
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
3078
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
1152
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 works fine. How can I add another DLL search directory for ASP.Net? I don't want to copy everything...
1
1906
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
7576
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. HOSTNAME is not one of those variables, it is set even in cron's subshell. Why doesn't python get...
0
9843
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10875
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10623
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10272
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7806
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5669
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5848
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4040
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.