By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,012 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,012 IT Pros & Developers. It's quick & easy.

How to 'set' an environment variable?

P: n/a
All:

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

Thanks,
Bill
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Bill,

Thanks for posting.

System environment variables are stored in the following key:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Se ssion 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.

Nov 15 '05 #2

P: n/a
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
"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:UV**************@cpmsftngxa07.phx.gbl...
Hi Bill,

Thanks for posting.

System environment variables are stored in the following key:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Se ssion 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.

Nov 15 '05 #3

P: n/a
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Bill Davidson" <bi***********@getronics.com> wrote in message
news:uZ**************@TK2MSFTNGP10.phx.gbl...
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
"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:UV**************@cpmsftngxa07.phx.gbl...
Hi Bill,

Thanks for posting.

System environment variables are stored in the following key:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Se ssion

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.


Nov 15 '05 #4

P: n/a
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" <er**@cc.ensoft-software.com> wrote in message
news:#e**************@TK2MSFTNGP09.phx.gbl... 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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Bill Davidson" <bi***********@getronics.com> wrote in message
news:uZ**************@TK2MSFTNGP10.phx.gbl...
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
"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:UV**************@cpmsftngxa07.phx.gbl...
Hi Bill,

Thanks for posting.

System environment variables are stored in the following key:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Se ssion

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.



Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.