Connecting Tech Pros Worldwide Help | Site Map

PATH environment variable

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 16th, 2005, 01:26 PM
Guest
 
Posts: n/a
Default 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.

  #2  
Old November 16th, 2005, 01:26 PM
Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
Default Re: PATH environment variable

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]
- mvp@spam.guard.caspershouse.com

<anonymous@discussions.microsoft.com> wrote in message
news:324a01c4c046$9d78f450$a301280a@phx.gbl...[color=blue]
> 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.[/color]


  #3  
Old November 16th, 2005, 01:26 PM
Julie
Guest
 
Posts: n/a
Default Re: PATH environment variable

anonymous@discussions.microsoft.com wrote:[color=blue]
>
> 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.[/color]

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";
  #4  
Old November 16th, 2005, 01:26 PM
Guest
 
Posts: n/a
Default Re: PATH environment variable

Thanks for your reply Nicholas. Can you explain how we
can call the SetEnvironmentVariable through the P/Invoke
layer? Thanks a lot in advance.[color=blue]
>-----Original Message-----
> There is no managed way to set the environment[/color]
variable strings.[color=blue]
>Rather, you will have to call the SetEnvironmentVariable[/color]
function through[color=blue]
>the P/Invoke layer.
>
> If you are using .NET 2.0, then you can use the[/color]
static[color=blue]
>SetEnvironmentVariable method on the Environment class.
>
> Hope this helps.
>
>
>--
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
><anonymous@discussions.microsoft.com> wrote in message
>news:324a01c4c046$9d78f450$a301280a@phx.gbl...[color=green]
>> Hi all, I am trying to append a certain string to the
>> PATH environment variable programmatically. I am able[/color][/color]
to[color=blue][color=green]
>> read what is in the variable using the[/color][/color]
System.Environment[color=blue][color=green]
>> method GetEnvironmentVariable("path"). However, I don't
>> know yet how to append strings to the path variable.[/color][/color]
Any[color=blue][color=green]
>> help is appreciated. Thanks a lot.[/color]
>
>
>.
>[/color]
  #5  
Old November 16th, 2005, 03:40 PM
Evan
Guest
 
Posts: n/a
Default RE: PATH environment variable

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);
}
}








"anonymous@discussions.microsoft.com" wrote:
[color=blue]
> 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.
>[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.