Setting Environment Variables in .NET
I need a way of setting environment variables on the fly in C#.
I have found a way using the registry but I have to reboot my machine before it becomes available to read. See below
===============
public static bool setEnvVar(string name, string Value)
{
try
{
//check the parameters
if(name!=null && Value!=null)
{
RegistryKey key1=Registry.LocalMachine .CreateSubKey(@"SYSTEM\ControlSet001\Control\Session Manager\Environment"); ;
key1.SetValue(name,Value);
return true;
}
else
{
MessageBox.Show("The parameters for the setEnvVar function were invalid");
return false;
}
}
catch(Exception ex)
{
MessageBox.Show("Exception in setEnvVar ");
MessageBox.Show(ex.Message);
return false;
}
}
==============
Is there a way to do it so it become available straight away (like using the cmd window) ?
There only seems to be Reading Env Variables defined in .NET
Can someone help?