This documentation is archived and is not being maintained.

EnvironmentPermission Class

Controls access to system and user environment variables. This class cannot be inherited.

Namespace:  System.Security.Permissions
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class EnvironmentPermission _
	Inherits CodeAccessPermission _
	Implements IUnrestrictedPermission
'Usage
Dim instance As EnvironmentPermission

Environment variable names are designated by one or more case-insensitive name lists separated by semicolons, with separate lists for read and write access to the named variables. Write access includes the ability to create and delete environment variables as well as to change existing values.

Caution noteCaution:

EnvironmentPermission grants permission for access to the environment variable and its value. To deny access to a variable and its value, you must deny access to it and any other variable which contains the same value. For example, to Deny access to the TMP variable and its value, %USERPROFILE%\Local Settings\Temp, you must Deny access to TMP, TEMP, and any other variable that you can use to access that value. A better technique to deal with multiple paths is to use a combination of PermitOnly and Deny. For more information on this subject and the use of PermitOnly with Deny, see "Canonicalization Problems Using Deny" in Using the Deny Method.

The following code example demonstrates the behavior of the EnvironmentPermission methods.

The example is intended to show how the methods perform if you execute the methods from your code. In general, the methods of permission classes are used by the security infrastructure; they are not typically used in applications.

Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Collections


Public Class EnvironmentPermissionDemo

    ' IsSubsetOf determines whether the current permission is a subset of the specified permission. 
    Private Function IsSubsetOfDemo() As Boolean  
        Dim returnValue As Boolean = True 
        Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir")
        Dim envPerm2 As New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "TEMP")
        If envPerm1.IsSubsetOf(envPerm2) Then

            Console.WriteLine("'windir' is a subset of 'TEMP'" + vbLf)
        Else
            Console.WriteLine("windir" + " is not a subset of " + "TEMP" + vbLf)
        End If
        envPerm1.SetPathList(EnvironmentPermissionAccess.Read, "TEMP")

        If envPerm1.IsSubsetOf(envPerm2) Then

            Console.WriteLine("Read access is a subset of AllAccess" + vbLf)
        Else
            Console.WriteLine("Read access is not a subset of AllAccess" + vbLf)
        End If 

        Return returnValue

    End Function 'IsSubsetOfDemo

    ' Union creates a new permission that is the union of the current permission and the specified permission. 
    Private Function UnionDemo() As Boolean  
        Dim returnValue As Boolean = True 
        Dim envIdPerm3 As IPermission
        Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir")
        Dim envPerm2 As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP")
        envIdPerm3 = CType(envPerm1.Union(envPerm2), EnvironmentPermission)
        envIdPerm3 = envPerm1.Union(envPerm2)
        Console.WriteLine("The union of 'windir' and 'TEMP'" + " = " + _
        CType(envIdPerm3, EnvironmentPermission).GetPathList(EnvironmentPermissionAccess.Read).ToString())

        Return returnValue

    End Function 'UnionDemo

    ' Intersect creates and returns a new permission that is the intersection of 
    ' the current permission and the permission specified. 
    Private Function IntersectDemo() As Boolean  

        Dim envIdPerm3 As IPermission
        Dim returnValue As Boolean = True 
        Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir")
        Dim envPerm2 As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP")
        Try
            envIdPerm3 = CType(envPerm1.Intersect(envPerm2), EnvironmentPermission)
            If Not (envIdPerm3 Is Nothing) AndAlso Not (CType(envIdPerm3, _
            EnvironmentPermission).GetPathList(EnvironmentPermissionAccess.Read) Is Nothing) Then
                Console.WriteLine("The intersection of " + "windir" + " and " + "TEMP" + _
                " = " + CType(envIdPerm3, EnvironmentPermission).GetPathList(EnvironmentPermissionAccess.Read).ToString())
            Else
                Console.WriteLine("The intersection of " + "windir" + " and " + "TEMP" + " is null.")
            End If 
        Catch e As Exception
            Console.WriteLine("An exception was thrown for intersection : " + e.Message)
            returnValue = False 
        End Try 

        Return returnValue

    End Function 'IntersectDemo

    'Copy creates and returns an identical copy of the current permission. 
    Private Function CopyDemo() As Boolean  
        Dim returnValue As Boolean = True 
        Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir")
        Try 
            Dim envPerm2 As EnvironmentPermission = CType(envPerm1.Copy(), EnvironmentPermission)
            If Not (envPerm2 Is Nothing) Then
                Console.WriteLine("Result of copy = " + envPerm2.ToString() + vbLf)
            Else
                Console.WriteLine("Result of copy is null. " + vbLf)
            End If 
        Catch e As Exception
            Console.WriteLine(e)
        End Try 

        Return returnValue

    End Function 'CopyDemo

    ' ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs 
    ' a permission with the specified state from the XML encoding. 
    Private Function ToFromXmlDemo() As Boolean  
        Dim returnValue As Boolean = True 
        Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir")
        Dim envPerm2 As New EnvironmentPermission(PermissionState.None)
        envPerm2.FromXml(envPerm1.ToXml())
        Console.WriteLine("Result of ToFromXml = " + envPerm2.ToString() + vbLf)

        Return returnValue

    End Function 'ToFromXmlDemo

    ' AddPathList adds access for the specified environment variables to the existing state of the permission. 
    ' SetPathList Sets the specified access to the specified environment variables to the existing state 
    ' of the permission. 
    ' GetPathList gets all environment variables with the specified EnvironmentPermissionAccess. 
    Private Function SetGetPathListDemo() As Boolean  
        Try
            Console.WriteLine("********************************************************" + vbLf)
            Console.WriteLine("Creating an EnvironmentPermission with AllAccess rights for 'TMP'")
            Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "TMP")
            Console.WriteLine("Adding 'TEMP' to the write access list, and 'windir' to the read access list.")
            envPerm1.AddPathList(EnvironmentPermissionAccess.Write, "TEMP")
            envPerm1.AddPathList(EnvironmentPermissionAccess.Read, "windir")
            Console.WriteLine("Read access list before SetPathList = " + envPerm1.GetPathList(EnvironmentPermissionAccess.Read))
            Console.WriteLine("Setting read access to 'TMP'")
            envPerm1.SetPathList(EnvironmentPermissionAccess.Read, "TMP")
            Console.WriteLine("Read access list after SetPathList = " + envPerm1.GetPathList(EnvironmentPermissionAccess.Read))
            Console.WriteLine("Write access list = " + envPerm1.GetPathList(EnvironmentPermissionAccess.Write))
            Console.WriteLine("Write access environment variables = " + envPerm1.GetPathList(EnvironmentPermissionAccess.AllAccess))
        Catch e As ArgumentException
            ' EnvironmentPermissionAccess.AllAccess cannot be used as a parameter for GetPathList.
            Console.WriteLine("An ArgumentException occurred as a result of using AllAccess. " + _
            " This property cannot be used as a parameter in GetPathList, because it represents " + _
            "more than one type of environment variable : " + vbLf + e.Message)
        End Try 

        Return True 

    End Function 'SetGetPathListDemo

    ' Invoke all demos. 
    Public Function RunDemo() As Boolean  

        Dim ret As Boolean = True 
        Dim retTmp As Boolean 
        ' Call IsSubsetOf demo.
        retTmp = IsSubsetOfDemo()
        If retTmp Then

            Console.Out.WriteLine("IsSubset demo completed successfully.")
        Else
            Console.Out.WriteLine("IsSubset demo failed.")
        End If
        ret = retTmp AndAlso ret


        ' Call Union demo.
        retTmp = UnionDemo()
        If retTmp Then

            Console.Out.WriteLine("Union demo completed successfully.")
        Else
            Console.Out.WriteLine("Union demo failed.")
        End If
        ret = retTmp AndAlso ret


        ' Call Intersect demo.
        retTmp = IntersectDemo()
        If retTmp Then

            Console.Out.WriteLine("Intersect demo completed successfully.")
        Else
            Console.Out.WriteLine("Intersect demo failed.")
        End If
        ret = retTmp AndAlso ret




        ' Call Copy demo.
        retTmp = CopyDemo()
        If retTmp Then

            Console.Out.WriteLine("Copy demo completed successfully.")
        Else
            Console.Out.WriteLine("Copy demo failed.")
        End If
        ret = retTmp AndAlso ret


        ' Call ToFromXml demo.
        retTmp = ToFromXmlDemo()
        If retTmp Then

            Console.Out.WriteLine("ToFromXml demo completed successfully.")
        Else
            Console.Out.WriteLine("ToFromXml demo failed.")
        End If
        ret = retTmp AndAlso ret


        ' Call SetGetPathList demo.
        retTmp = SetGetPathListDemo()
        If retTmp Then

            Console.Out.WriteLine("SetGetPathList demo completed successfully.")
        Else
            Console.Out.WriteLine("SetGetPathList demo failed.")
        End If
        ret = retTmp AndAlso ret

        Return ret

    End Function 'RunDemo

    ' Test harness. 
    Public Shared Sub Main(ByVal args() As String) 
        Try 
            Dim democase As New EnvironmentPermissionDemo()
            Dim ret As Boolean = democase.RunDemo()
            If ret Then
                Console.Out.WriteLine("EnvironmentPermission demo completed successfully.")
                Console.Out.WriteLine("Press the Enter key to exit.")
                Dim consoleInput As String = Console.ReadLine()
                System.Environment.ExitCode = 100
            Else
                Console.Out.WriteLine("EnvironmentPermission demo failed.")
                Console.Out.WriteLine("Press the Enter key to exit.")
                Dim consoleInput As String = Console.ReadLine()
                System.Environment.ExitCode = 101
            End If 
        Catch e As Exception
            Console.Out.WriteLine("EnvironmentPermission demo failed.")
            Console.WriteLine(e.ToString())
            Console.Out.WriteLine("Press the Enter key to exit.")
            Dim consoleInput As String = Console.ReadLine()
            System.Environment.ExitCode = 101
        End Try 

    End Sub 'Main
End Class 'EnvironmentPermissionDemo

System.Object
  System.Security.CodeAccessPermission
    System.Security.Permissions.EnvironmentPermission

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: