Solved

setting environment variables in c#

Posted on 2004-03-26
4
3,105 Views
Last Modified: 2008-06-07
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 ?

Sam
0
Comment
Question by:sampathbwelagedera
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 10685703
User environment variables are kept in the Registry key:
HKEY_CURRENT_USER\Environment

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.
0
 
LVL 19

Expert Comment

by:Desp
ID: 10685858
use the SetEnvironmentVariable api..

using System.Runtime.InteropServices;
.......

[DllImport("Kernel32.dll")]
static extern bool SetEnvironmentVariable(string name, string val);
.........
bool result = SetEnvironmentVariable("Var_Name", "Var_Value");
0
 

Author Comment

by:sampathbwelagedera
ID: 10721995
I Tryied Ales's method it works fine thanks for the tip.
 i'll try the method Desp had suggested as well.

/Sam
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10722082
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question