Solved

setting environment variables in c#

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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