473,387 Members | 1,760 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,387 software developers and data experts.

PATH environment variable

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 GetEnvironmentVariable("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 39004
There is no managed way to set the environment variable strings.
Rather, you will have to call the SetEnvironmentVariable function through
the P/Invoke layer.

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

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

<an*******@discussions.microsoft.com> wrote in message
news:32****************************@phx.gbl...
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 GetEnvironmentVariable("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*******@discussions.microsoft.com wrote:

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 GetEnvironmentVariable("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%\subfolder\yourapplication.exe";
string expanded = System.Environment.ExpandEnvironmentVairables(path );

Other way:

string expanded = System.Environment.GetEnvironmentVariable("path") +
@"\subfolder\yourapplication.exe";
Nov 16 '05 #3
Thanks for your reply Nicholas. Can you explain how we
can call the SetEnvironmentVariable 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 SetEnvironmentVariable function throughthe P/Invoke layer.

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

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

<an*******@discussions.microsoft.com> wrote in message
news:32****************************@phx.gbl...
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 GetEnvironmentVariable("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("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg,
UIntPtr wParam, string lParam, SendMessageTimeoutFlags fuFlags,
uint uTimeout, out UIntPtr lpdwResult);

public static string RegKey = @"System\CurrentControlSet\Control\Session
Manager\Environment";

public static string NewPathForFolder = "d:\bin";
public enum SendMessageTimeoutFlags:uint
{
SMTO_NORMAL= 0x0000
, SMTO_BLOCK = 0x0001
, SMTO_ABORTIFHUNG = 0x0002
, SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
}

private static void SetEnvironmentPath()
{
// Update registry with environment variable path
RegistryKey key;
key = Registry.LocalMachine.OpenSubKey(RegKey,true);
string path = (String)key.GetValue("Path");
string TestPath = NewPathForFolder;
int a = path.IndexOf(TestPath,1);
if (a == -1)
{
path += ";" + NewFolderForPath;
key.SetValue("Path", path);

//Notify all windows that User Environment variables are changed
IntPtr HWND_BROADCAST = (IntPtr) 0xffff;
const UInt32 WM_SETTINGCHANGE = 0x001A;
UIntPtr result;
IntPtr settingResult = SendMessageTimeout(HWND_BROADCAST,
WM_SETTINGCHANGE,
(UIntPtr)0,
"Environment",
SendMessageTimeoutFlags.SMTO_NORMAL,
10000,
out result);
}
}


"an*******@discussions.microsoft.com" wrote:
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 GetEnvironmentVariable("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
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: 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....
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...
1
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...
1
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"),...
10
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...
0
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...
1
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.