setting environment variables in c#

I need to set an environment variable (give a new one) through c# coding in the installation process. the Environment class (system.environment) doesnt allow to do that, although environment variables can be read from it.
is there any way to do this. some other class which can do this or an work around ?

Who is Participating?
AlexFMConnect With a Mentor Commented:
User environment variables are kept in the Registry key:

System environment variables are kept in the Registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

You need to set them using Registry functions.
Fahad MukhtarDistinguished EngineerCommented:
use the SetEnvironmentVariable api..

using System.Runtime.InteropServices;

static extern bool SetEnvironmentVariable(string name, string val);
bool result = SetEnvironmentVariable("Var_Name", "Var_Value");
sampathbwelagederaAuthor Commented:
I Tryied Ales's method it works fine thanks for the tip.
 i'll try the method Desp had suggested as well.

SetEnvironmentVariable works for the current process and for processes launched from it. It is not persistent. It depends on your purposes, which way to use.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.