Solved

setting environment variables in c#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Customize & Tweak Windows 10 Installation 14 38
Image decoding from Camera 3 72
How can i compile this github project?? 2 74
C# code editing and collaboration 3 98
A short article about a problem I had getting the GPS LocationListener working.
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.
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

15 Experts available now in Live!

Get 1:1 Help Now