Solved

setting environment variables in c#

Posted on 2004-03-26
4
3,101 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now