214 ❘
CHAPTER 10 Using ironPython for AdministrAtion tAsks
Using the .NET method Working with environment variables using the .NET method isn’t nearly as easy as working
with them using the Python method. Then again, you can make permanent environment variable
changes using .NET. In fact, .NET provides support for three levels of environment variables.
➤➤
Process: Affects only the current process and any processes that the current process starts
➤
➤
User: Affects only the current user
➤
➤
Machine: Affects all users of the host system
An important difference between the Python and .NET methods is that any change you make using
the .NET method affects both command line and graphical applications. You have significant control
over precisely how and where an environment variable change appears because you specify precisely
what level the environment variable should affect. The following sections provide more information
on reading and setting environment variables using the .NET method.
Reading the Environment Variables Using .NET As previously mentioned, the .NET method is more flexible than the Python method, but also
requires a little extra work on your part. Some of the extra work comes in the form of flexibility.
The .NET method provides several ways to obtain environment variable data.
➤➤
Use one of the
Environment
class properties to obtain a standard environment variable
value. You can find a list of these properties at
http://msdn.microsoft.com/library/
system.environment_properties.aspx
.
➤
➤
Check a specific environment variable using
GetEnvironmentVariable()
.
➤
➤
Obtain all the environment variables for a particular level using
GetEnvironmentVariables()
with an
EnvironmentVariableTarget
enumeration value.
➤
➤
Obtain all the environment variables regardless of level using
GetEnvironmentVariables()
.
It’s important to note that these techniques let you answer questions such as whether a particular envi-
ronment variable is a standard or custom setting. You can also determine whether the environment
variable affects the process, user, or machine as a whole. In short, you obtain more information using
the .NET method, but at the cost of additional complexity. Listing 10-8 shows how to read environ-
ment variables using each of the .NET methods.