How to 'set' an environment variable?

B

Bill Davidson

All:

I've found the 'Environment.GetEnvironmentVariable()' method; but how do I
create and/or set an environment variable?

Thanks,
Bill
 
F

Felix Wang

Hi Bill,

Thanks for posting.

System environment variables are stored in the following key:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment

User variables are stored in the following key:

HKEY_CURRENT_USER\Environment

Maybe we can use the Microsoft.Win32.Registry and the RegistryKey classes
to manipulate them directly.

I hope this makes sense to you.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bill Davidson

Thanks,

Perhaps I'll just call the SetEnvironmentVariable() Win32 API via P/Invoke.
I just figured the Framework Class Libraries wrapped this call, but I just
couldn't find it.

Thanks for your help,
Bill
 
E

Eric Newton

The reason why you're probably never gonna see SetEnvironmentVariable in te
framework is due to the encouragement of utilizing config files as your
settings. Of course, we as developers can fight that by adding things back
in, but the focus has shifted from Environement Variables -> Registry ->
config files, and I think its a good change... its a whole lot easier to
shift config files around then it is to write a batch file or a "Reg Import"
when you want to duplicate or just move apps around.
 
B

Bill Davidson

Eric:
The reason why you're probably never gonna see SetEnvironmentVariable in te
framework is due to the encouragement of utilizing config files as your
settings

I figured this might be the case, but the Framework provides the
EnvironmentPermission class with the EnvironmentPermissionAccess enumeration
that contains the 'Write' Permission defined as follows: "Write access to
environment variables is specified. Write access includes creating and
deleting environment variables as well as changing existing values. Reading
environment variables is not included in this access level".

'Seems like the .Net security model is all set up for handling environment
variables, but the methods for actually writing/creating these variables are
nowhere to be found.

Anyway, I found a workaround so I'll close this thread. Thanks to everyone
for their assistance.

Bill



Eric Newton said:
The reason why you're probably never gonna see SetEnvironmentVariable in te
framework is due to the encouragement of utilizing config files as your
settings. Of course, we as developers can fight that by adding things back
in, but the focus has shifted from Environement Variables -> Registry ->
config files, and I think its a good change... its a whole lot easier to
shift config files around then it is to write a batch file or a "Reg Import"
when you want to duplicate or just move apps around.

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Bill Davidson said:
Thanks,

Perhaps I'll just call the SetEnvironmentVariable() Win32 API via P/Invoke.
I just figured the Framework Class Libraries wrapped this call, but I just
couldn't find it.

Thanks for your help,
Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top