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]
- m...@spam.guard.caspershouse.com
<anon...@discussions.microsoft.com> wrote in message
news:324a01c4c046$9d78f450$a301...@phx.gbl...
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";
// 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);