EnvironmentPermission-Klasse
Steuert den Zugriff auf System- und Benutzerumgebungsvariablen. Diese Klasse kann nicht vererbt werden.
Namespace: System.Security.Permissions
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class EnvironmentPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
'Usage
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
Hinweise
Umgebungsvariablennamen werden, durch Semikolon getrennt und ohne Beachtung der Groß- und Kleinschreibung, in einer oder mehreren Namenslisten ausgewiesen, wobei getrennte Listen für den Lese- und Schreibzugriff auf die benannten Variablen verwendet werden. Der Schreibzugriff umfasst das Erstellen und Löschen von Umgebungsvariablen sowie das Ändern vorhandener Werte.
Warnung
EnvironmentPermission gewährt die Berechtigung für den Zugriff auf die Umgebungsvariable und ihren Wert. Den Zugriff auf eine Variable und ihren Wert verweigern Sie, indem Sie den Zugriff auf diese und alle anderen Variablen verweigern, die diesen Wert enthalten. Um z. B. den Zugriff auf die TMP-Variable und ihren Wert %USERPROFILE%\Local Settings\Temp mit Deny zu verweigern, müssen Sie den Zugriff auf TMP, TEMP und andere Variablen, mit denen auf den Wert zugegriffen werden kann, mit Deny verweigern. Ein einfacheres Verfahren für den Umgang mit mehreren Pfaden besteht in der Kombination aus PermitOnly und Deny. Weitere Informationen über dieses Thema und über die Verwendung von PermitOnly mit Deny finden Sie unter "Kanonisierungsprobleme beim Verwenden von Deny" in Verwenden der Deny-Methode.
Beispiel
Im folgenden Codebeispiel wird das Verhalten der EnvironmentPermission-Methoden veranschaulicht. Dieses Beispiel soll lediglich die Ergebnisse der Methoden veranschaulichen, nicht deren Verwendung.
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;
}
}
}
Vererbungshierarchie
System.Object
System.Security.CodeAccessPermission
System.Security.Permissions.EnvironmentPermission
Threadsicherheit
Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Plattformen
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 unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
EnvironmentPermission-Member
System.Security.Permissions-Namespace
EnvironmentPermissionAttribute
EnvironmentPermissionAccess