다음을 통해 공유


EnvironmentPermission 클래스

시스템 및 사용자 환경 변수에 대한 액세스를 제어합니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: System.Security.Permissions
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class EnvironmentPermission
    Inherits CodeAccessPermission
    Implements IUnrestrictedPermission
‘사용 방법
Dim instance As EnvironmentPermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class EnvironmentPermission : CodeAccessPermission, IUnrestrictedPermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class EnvironmentPermission sealed : public CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class EnvironmentPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class EnvironmentPermission extends CodeAccessPermission implements IUnrestrictedPermission

설명

환경 변수 이름은 대/소문자를 구분하지 않고 하나 이상의 이름을 세미콜론으로 구분한 이름 목록에서 지정되며, 각 목록은 이름이 지정된 변수에 대해 읽기 및 쓰기 권한을 가지도록 지정됩니다. 쓰기 권한에는 기존 값을 변경하는 기능은 물론 환경 변수를 만들고 삭제하는 기능도 포함됩니다.

경고

EnvironmentPermission은 환경 변수와 그 값에 액세스할 수 있는 권한을 부여합니다. 변수와 그 값에 대한 액세스를 거부하려면, 해당 변수 및 그와 동일한 값이 포함된 모든 변수에 대한 액세스를 거부해야 합니다. 예를 들어 TMP 변수와 그 값인 %USERPROFILE%\Local Settings\Temp에 대한 액세스를 Deny하려면 TMP, TEMP 및 해당 값에 액세스하는 데 사용할 수 있는 기타 모든 변수에 대한 액세스를 Deny해야 합니다. 경로가 여러 개인 경우 PermitOnlyDeny를 함께 사용하는 것이 좋습니다. 이 주제 및 Deny와 함께 PermitOnly를 사용하는 방법에 대한 자세한 내용은 Deny 메서드 사용에서 "Canonicalization Problems Using Deny"를 참조하십시오.

예제

다음 코드 예제에서는 EnvironmentPermission 메서드의 동작을 보여 줍니다. 이 예제는 메서드의 결과를 보여주기 위한 것이지 메서드를 사용하는 방법을 보여주기 위한 것이 아닙니다.

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


using System;
using System.Security;
using System.Security.Permissions;
using System.Collections;

[assembly: CLSCompliant(true)]

public class EnvironmentPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    private bool IsSubsetOfDemo()
    {
        bool returnValue = true;
        EnvironmentPermission envPerm1 = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir");
        EnvironmentPermission envPerm2 = new EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "TEMP");
        if (envPerm1.IsSubsetOf(envPerm2))
        {

            Console.WriteLine("'windir' is a subset of 'TEMP'\n");
        }
        else
        {
            Console.WriteLine("windir" + " is not a subset of "
                + "TEMP" + "\n");
        }
        envPerm1.SetPathList(EnvironmentPermissionAccess.Read, "TEMP");

        if (envPerm1.IsSubsetOf(envPerm2))
        {

            Console.WriteLine("Read access is a subset of AllAccess\n");
        }
        else
        {
            Console.WriteLine("Read access is not a subset of AllAccess\n");
        }

        return returnValue;
    }
    // Union creates a new permission that is the union of the current permission and the specified permission.
    private bool UnionDemo()
    {
        bool returnValue = true;
        IPermission envIdPerm3;
        EnvironmentPermission envPerm1 = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir");
        EnvironmentPermission envPerm2 = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP");
        envIdPerm3 = (EnvironmentPermission)envPerm1.Union(envPerm2);
        envIdPerm3 = envPerm1.Union(envPerm2);
        Console.WriteLine("The union of 'windir' and 'TEMP'" +
            " = " + ((EnvironmentPermission)envIdPerm3).GetPathList(EnvironmentPermissionAccess.Read).ToString());

        return returnValue;

    }
    // Intersect creates and returns a new permission that is the intersection of
    // the current permission and the permission specified.
    private bool IntersectDemo()
    {

        IPermission envIdPerm3;
        bool returnValue = true;
        EnvironmentPermission envPerm1 = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir");
        EnvironmentPermission envPerm2 = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP");
        try
        {
            envIdPerm3 = (EnvironmentPermission)envPerm1.Intersect(envPerm2);
            if (envIdPerm3 != null && ((EnvironmentPermission)envIdPerm3).GetPathList(EnvironmentPermissionAccess.Read) != null)
            {
                Console.WriteLine("The intersection of " + "windir" + " and " + "TEMP" +                    " = " + ((EnvironmentPermission)envIdPerm3).GetPathList(EnvironmentPermissionAccess.Read).ToString());
            }
            else
            {
                Console.WriteLine("The intersection of " + "windir" + " and "
                    + "TEMP" + " is null.");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("An exception was thrown for intersection : " + e);
            returnValue = false;
        }
    
        return returnValue;

    }
    //Copy creates and returns an identical copy of the current permission.
    private bool CopyDemo()
    {
        bool returnValue = true;
        EnvironmentPermission envPerm1 = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir");
        try
        {
            EnvironmentPermission envPerm2 = (EnvironmentPermission)envPerm1.Copy();
            if (envPerm2 != null)
            {
                Console.WriteLine("Result of copy = " + envPerm2.ToString() + "\n");
            }
            else
            {
                Console.WriteLine("Result of copy is null. \n");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        return returnValue;
    }
    // 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 bool ToFromXmlDemo()
    {
        bool returnValue = true;
        EnvironmentPermission envPerm1 = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "windir");
        EnvironmentPermission envPerm2 = new EnvironmentPermission(PermissionState.None);
        envPerm2.FromXml(envPerm1.ToXml());
        Console.WriteLine("Result of ToFromXml = " + envPerm2.ToString() + "\n");

        return returnValue;

    }
    // 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 bool SetGetPathListDemo()
    {
        try
        {
            Console.WriteLine("********************************************************\n");
            Console.WriteLine("Creating an EnvironmentPermission with AllAccess rights for 'TMP'");
            EnvironmentPermission envPerm1 = 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 (ArgumentException e)
        {
            // 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 : \n" + e);
        }

        return true;
    }
    // Invoke all demos.
    public bool RunDemo()
    {

        bool ret = true;
        bool retTmp;
        // Call IsSubsetOf demo.
        if (retTmp = IsSubsetOfDemo()) Console.Out.WriteLine("IsSubset demo completed successfully.");
        else Console.Out.WriteLine("IsSubset demo failed.");
        ret = retTmp && ret;

        // Call Union demo.
        if (retTmp = UnionDemo()) Console.Out.WriteLine("Union demo completed successfully.");
        else Console.Out.WriteLine("Union demo failed.");
        ret = retTmp && ret;

        // Call Intersect demo.
        if (retTmp = IntersectDemo()) Console.Out.WriteLine("Intersect demo completed successfully.");
        else Console.Out.WriteLine("Intersect demo failed.");
        ret = retTmp && ret;


        // Call Copy demo.
        if (retTmp = CopyDemo()) Console.Out.WriteLine("Copy demo completed successfully.");
        else Console.Out.WriteLine("Copy demo failed.");
        ret = retTmp && ret;

        // Call ToFromXml demo.
        if (retTmp = ToFromXmlDemo()) Console.Out.WriteLine("ToFromXml demo completed successfully.");
        else Console.Out.WriteLine("ToFromXml demo failed.");
        ret = retTmp && ret;

        // Call SetGetPathList demo.
        if (retTmp = SetGetPathListDemo()) Console.Out.WriteLine("SetGetPathList demo completed successfully.");
        else Console.Out.WriteLine("SetGetPathList demo failed.");
        ret = retTmp && ret;

        return (ret);

    }
    // Test harness.
    public static void Main(String[] args)
    {
        try
        {
            EnvironmentPermissionDemo democase = new EnvironmentPermissionDemo();
            bool ret = democase.RunDemo();
            if (ret)
            {
                Console.Out.WriteLine("EnvironmentPermission demo completed successfully.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 100;
            }
            else
            {
                Console.Out.WriteLine("EnvironmentPermission demo failed.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 101;
            }
        }
        catch (Exception e)
        {
            Console.Out.WriteLine("EnvironmentPermission demo failed.");
            Console.WriteLine(e.ToString());
            Console.Out.WriteLine("Press the Enter key to exit.");
            string consoleInput = Console.ReadLine();
            System.Environment.ExitCode = 101;
        }
    }
}


상속 계층 구조

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

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

EnvironmentPermission 멤버
System.Security.Permissions 네임스페이스
EnvironmentPermissionAttribute
EnvironmentPermissionAccess

기타 리소스

보안 권한
권한 요청