EnvironmentPermission クラス
システム環境変数およびユーザー環境変数へのアクセスを制御します。このクラスは継承できません。
この型のすべてのメンバの一覧については、EnvironmentPermission メンバ を参照してください。
System.Object
System.Security.CodeAccessPermission
System.Security.Permissions.EnvironmentPermission
<Serializable>
NotInheritable Public Class EnvironmentPermission Inherits CodeAccessPermission Implements IUnrestrictedPermission
[C#]
[Serializable]
public sealed class EnvironmentPermission : CodeAccessPermission, IUnrestrictedPermission
[C++]
[Serializable]
public __gc __sealed class EnvironmentPermission : public CodeAccessPermission, IUnrestrictedPermission
[JScript]
public
Serializable
class EnvironmentPermission extends CodeAccessPermission implements IUnrestrictedPermission
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
環境変数名は、大文字と小文字を区別せずに、名前をセミコロンで区切った 1 つ以上のリストによって指定されます。指定した変数への読み取り用と書き込み用とで個別のリストを用意します。書き込みアクセスでは、既存の値を変更する上に、環境変数の作成および削除ができます。
注意 EnvironmentPermission は、環境変数とその値にアクセスするためのアクセス許可を与えます。環境変数とその値へのアクセスを拒否するには、同じ値を格納する他の変数へのアクセスも拒否する必要があります。たとえば、TMP 環境変数とその値 (%USERPROFILE%\Local Settings\Temp) へのアクセスを Deny によって拒否するには、TMP、TEMP を含め、この値にアクセスするために使用できるその他のすべての変数へのアクセスも Deny によって拒否する必要があります。複数のパスを使用する場合は、 PermitOnly と Deny を組み合わせて使用することをお勧めします。この詳細および Deny で PermitOnly を使用する場合の詳細については、「 Deny 」の Canonicalization Problems Using Deny を参照してください。
使用例
' This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods
' of the EnvironmentPermission class.
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Collections
Imports Microsoft.VisualBasic
<Assembly: CLSCompliant(True)>
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 env1, env2 As String
Dim envPerm1, envPerm2 As EnvironmentPermission
Dim envGen1 As New EnvGenerator()
Dim envGen2 As New EnvGenerator()
envGen1.ResetIndex()
While envGen1.CreateEnv(envPerm1, env1, EnvironmentPermissionAccess.Read)
If envPerm1 Is Nothing Then
GoTo ContinueWhile1
End If
Console.WriteLine("**********************************************************" & ControlChars.Lf)
envGen2.ResetIndex()
While envGen2.CreateEnv(envPerm2, env2, EnvironmentPermissionAccess.AllAccess)
Dim firstPermission As String = IIf(env1 = "" Or env1 Is Nothing, "null", env1)
Dim secondPermission As String = IIf(env2 = "" Or env2 Is Nothing, "null", env2)
If envPerm2 Is Nothing Then
GoTo ContinueWhile2
End If
Try
If envPerm1.IsSubsetOf(envPerm2) Then
Console.WriteLine((firstPermission & " is a subset of " & secondPermission & ControlChars.Lf))
Else
Console.WriteLine((firstPermission & " is not a subset of " & secondPermission & ControlChars.Lf))
End If
Catch e As Exception
Console.WriteLine(IIf("An exception was thrown for subset :" & env1 = "", "null", IIf(env1 & ControlChars.Lf & env2 = "", "null.", env2 & ControlChars.Lf & e.ToString())))
End Try
ContinueWhile2:
End While
ContinueWhile1:
End While
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 env1, env2 As String
Dim envPerm1, envPerm2 As EnvironmentPermission
Dim envIdPerm3 As IPermission
Dim envGen1 As New EnvGenerator()
Dim envGen2 As New EnvGenerator()
envGen1.ResetIndex()
While envGen1.CreateEnv(envPerm1, env1, EnvironmentPermissionAccess.Read)
If env1 Is Nothing Then
GoTo ContinueWhile1
End If
Console.WriteLine("**********************************************************" & ControlChars.Lf)
envGen2.ResetIndex()
While envGen2.CreateEnv(envPerm2, env2, EnvironmentPermissionAccess.Read)
Try
If envPerm2 Is Nothing Then
GoTo ContinueWhile2
End If
Dim firstPermission As String = IIf(env1 = "" Or env1 Is Nothing, "null", env1)
Dim secondPermission As String = IIf(env2 = "" Or env2 Is Nothing, "null", env2)
envIdPerm3 = CType(envPerm1.Union(envPerm2), EnvironmentPermission)
envIdPerm3 = envPerm1.Union(envPerm2)
If envIdPerm3 Is Nothing Then
Console.WriteLine(("The union of " & firstPermission & " and " & secondPermission & " is null."))
Else
Console.WriteLine(("The union of " & firstPermission & " and " & secondPermission & " = " & CType(envIdPerm3, EnvironmentPermission).GetPathList(EnvironmentPermissionAccess.Read).ToString()))
End If
Catch e As Exception
Console.WriteLine(("An exception was thrown for union " & e.ToString()))
returnValue = False
End Try
ContinueWhile2:
End While
ContinueWhile1:
End While
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 returnValue As Boolean = True
Dim env1, env2 As String
Dim envPerm1, envPerm2, envIdPerm3 As EnvironmentPermission
Dim envGen1 As New EnvGenerator()
Dim envGen2 As New EnvGenerator()
envGen1.ResetIndex()
While envGen1.CreateEnv(envPerm1, env1, EnvironmentPermissionAccess.Read)
If env1 Is Nothing Then
GoTo ContinueWhile1
End If
Console.WriteLine("**********************************************************" & ControlChars.Lf)
envGen2.ResetIndex()
While envGen2.CreateEnv(envPerm2, env2, EnvironmentPermissionAccess.Read)
If envPerm2 Is Nothing Then
GoTo ContinueWhile2
End If
Dim firstPermission As String = IIf(env1 = "" Or env1 Is Nothing, "null", env1)
Dim secondPermission As String = IIf(env2 = "" Or env2 Is Nothing, "null", env2)
Try
envIdPerm3 = CType(envPerm1.Intersect(envPerm2), EnvironmentPermission)
If Not (envIdPerm3 Is Nothing) AndAlso Not (envIdPerm3.GetPathList(EnvironmentPermissionAccess.Read) Is Nothing) Then
Console.WriteLine(("The intersection of " & firstPermission & " and " & secondPermission & " = " & CType(envIdPerm3, EnvironmentPermission).GetPathList(EnvironmentPermissionAccess.Read).ToString()))
Else
Console.WriteLine(("The intersection of " & firstPermission & " and " & secondPermission & " is null."))
End If
Catch e As Exception
Console.WriteLine(("An exception was thrown for intersection : " & e.ToString()))
returnValue = False
End Try
ContinueWhile2:
End While
ContinueWhile1:
End While
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 env1 As String
Dim envPerm1, envPerm2 As EnvironmentPermission
Dim envGen1 As New EnvGenerator()
Dim envGen2 As New EnvGenerator()
envGen1.ResetIndex()
While envGen1.CreateEnv(envPerm1, env1, EnvironmentPermissionAccess.Read)
If env1 Is Nothing Then
GoTo ContinueWhile1
End If
Console.WriteLine("**********************************************************" & ControlChars.Lf)
envGen2.ResetIndex()
Try
envPerm2 = CType(envPerm1.Copy(), EnvironmentPermission)
If Not (envPerm2 Is Nothing) Then
Console.WriteLine(("Result of copy = " & envPerm2.ToString() & ControlChars.Lf))
Else
Console.WriteLine("Result of copy is null. " & ControlChars.Lf)
End If
Catch e As Exception
If (True.ToString()) Then
If env1 = "" Then
Console.WriteLine("The EnvironmentPermission is empty, copy failed.")
Else
Console.WriteLine(e.ToString())
End If
End If
GoTo ContinueWhile1
End Try
ContinueWhile1:
End While
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 env1 As String
Dim envPerm1, envPerm2 As EnvironmentPermission
Dim envGen1 As New EnvGenerator()
Dim envGen2 As New EnvGenerator()
envGen1.ResetIndex()
While envGen1.CreateEnv(envPerm1, env1, EnvironmentPermissionAccess.Read)
If envPerm1 Is Nothing Then
GoTo ContinueWhile1
End If
Console.WriteLine("********************************************************" & ControlChars.Lf)
envGen2.ResetIndex()
Try
envPerm2 = New EnvironmentPermission(PermissionState.None)
envPerm2.FromXml(envPerm1.ToXml())
Console.WriteLine(("Result of ToFromXml = " & envPerm2.ToString() & ControlChars.Lf))
Catch e As Exception
Console.WriteLine(("ToFromXml failed :" & envPerm1.ToString() & e.ToString()))
GoTo ContinueWhile1
End Try
ContinueWhile1:
End While
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("********************************************************" & ControlChars.Lf)
Dim envPerm1 As EnvironmentPermission
Console.WriteLine("Creating an EnvironmentPermission with AllAccess rights for 'TMP'")
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 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 : " & ControlChars.Lf & e.ToString()))
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 Overloads 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.")
System.Environment.ExitCode = 100
Console.Out.WriteLine("Press the Enter key to exit.")
Dim consoleInput As String = Console.ReadLine()
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
' This class generates EnvironmentPermission objects.
Friend Class EnvGenerator
Private myEnv As String() = {"windir", "TMP", "TEMP"}
Private envIndex As Integer = 0
Public Sub New()
ResetIndex()
End Sub 'New
Public Sub ResetIndex()
envIndex = 0
End Sub 'ResetIndex
' Create an EnvironmentPermission using a given EnvironmentPermissionAccess.
Public Function CreateEnv(ByRef envPerm As EnvironmentPermission, ByRef env As String, ByVal epa As EnvironmentPermissionAccess) As Boolean
If envIndex = myEnv.Length Then
envPerm = New EnvironmentPermission(PermissionState.None)
env = ""
envIndex = envIndex + 1
Return True
End If
If envIndex > myEnv.Length Then
envPerm = Nothing
env = ""
Return False
End If
env = myEnv(envIndex)
envIndex = envIndex + 1
Try
envPerm = New EnvironmentPermission(epa, env)
Return True
Catch e As Exception
Console.WriteLine(("Cannot create EnvironmentPermission : " & env & " " & e.ToString()))
envPerm = New EnvironmentPermission(PermissionState.None)
env = ""
Return True
End Try
End Function 'CreateEnv
' End of EnvGenerator.
[C#]
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods
// of the EnvironmentPermission class.
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;
string env1,env2;
EnvironmentPermission envPerm1,envPerm2;
EnvGenerator envGen1 = new EnvGenerator();
EnvGenerator envGen2 = new EnvGenerator();
envGen1.ResetIndex();
while(envGen1.CreateEnv(out envPerm1, out env1, EnvironmentPermissionAccess.Read))
{
if(envPerm1 == null) continue;
Console.WriteLine("**********************************************************\n");
envGen2.ResetIndex();
while(envGen2.CreateEnv(out envPerm2, out env2, EnvironmentPermissionAccess.AllAccess))
{
string firstPermission = env1 == "" | env1 == null ? "null" : env1 ;
string secondPermission = env2 == "" | env2 == null ? "null" : env2;
if(envPerm2 == null) continue;
try
{
if (envPerm1.IsSubsetOf(envPerm2))
{
Console.WriteLine(firstPermission +" is a subset of "
+ secondPermission + "\n");
}
else
{
Console.WriteLine(firstPermission +" is not a subset of "
+ secondPermission + "\n");
}
}
catch(Exception e)
{
Console.WriteLine("An exception was thrown for subset :"
+ env1 == "" ? "null" : env1 + "\n" + env2 == "" ? "null." : env2 +"\n" + e);
}
}
}
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;
string env1,env2;
EnvironmentPermission envPerm1,envPerm2;
IPermission envIdPerm3;
EnvGenerator envGen1 = new EnvGenerator();
EnvGenerator envGen2 = new EnvGenerator();
envGen1.ResetIndex();
while(envGen1.CreateEnv(out envPerm1, out env1, EnvironmentPermissionAccess.Read))
{
if(env1 == null) continue;
Console.WriteLine("**********************************************************\n");
envGen2.ResetIndex();
while(envGen2.CreateEnv(out envPerm2, out env2, EnvironmentPermissionAccess.Read))
{
try
{
if(envPerm2 == null) continue;
string firstPermission = env1 == "" | env1 == null ? "null" : env1 ;
string secondPermission = env2 == "" | env2 == null ? "null" : env2;
envIdPerm3 = (EnvironmentPermission)envPerm1.Union(envPerm2);
envIdPerm3 = envPerm1.Union(envPerm2);
if(envIdPerm3 == null)
{
Console.WriteLine("The union of " + firstPermission + " and "
+ secondPermission + " is null.");
}
else
{
Console.WriteLine("The union of " + firstPermission + " and "
+ secondPermission +
" = " + ((EnvironmentPermission)envIdPerm3).GetPathList(EnvironmentPermissionAccess.Read).ToString());
}
}
catch(Exception e)
{
Console.WriteLine("An exception was thrown for union " + e);
returnValue=false;
}
}
}
return returnValue;
}
// Intersect creates and returns a new permission that is the intersection of
// the current permission and the permission specified.
private bool IntersectDemo()
{
bool returnValue = true;
string env1,env2;
EnvironmentPermission envPerm1,envPerm2,envIdPerm3;
EnvGenerator envGen1 = new EnvGenerator();
EnvGenerator envGen2 = new EnvGenerator();
envGen1.ResetIndex();
while(envGen1.CreateEnv(out envPerm1, out env1, EnvironmentPermissionAccess.Read))
{
if(env1 == null) continue;
Console.WriteLine("**********************************************************\n");
envGen2.ResetIndex();
while(envGen2.CreateEnv(out envPerm2, out env2, EnvironmentPermissionAccess.Read))
{
if(envPerm2 == null) continue;
string firstPermission = env1 == "" | env1 == null ? "null" : env1 ;
string secondPermission = env2 == "" | env2 == null ? "null" : env2;
try
{
envIdPerm3 = (EnvironmentPermission)envPerm1.Intersect(envPerm2);
if (envIdPerm3 != null && envIdPerm3.GetPathList(EnvironmentPermissionAccess.Read) != null)
{
Console.WriteLine("The intersection of " + firstPermission + " and " + secondPermission +
" = " + ((EnvironmentPermission)envIdPerm3).GetPathList(EnvironmentPermissionAccess.Read).ToString());
}
else
{
Console.WriteLine("The intersection of " + firstPermission + " and "
+ secondPermission + " 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;
string env1;
EnvironmentPermission envPerm1,envPerm2;
EnvGenerator envGen1 = new EnvGenerator();
EnvGenerator envGen2 = new EnvGenerator();
envGen1.ResetIndex();
while(envGen1.CreateEnv(out envPerm1, out env1, EnvironmentPermissionAccess.Read))
{
if(env1 == null ) continue;
Console.WriteLine("**********************************************************\n");
envGen2.ResetIndex();
try
{
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)
{
{
if (env1 == "")
{
Console.WriteLine( "The EnvironmentPermission is empty, copy failed.");
}
else
Console.WriteLine( e);
}
continue;
}
}
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;
string env1;
EnvironmentPermission envPerm1,envPerm2;
EnvGenerator envGen1 = new EnvGenerator();
EnvGenerator envGen2 = new EnvGenerator();
envGen1.ResetIndex();
while(envGen1.CreateEnv(out envPerm1, out env1, EnvironmentPermissionAccess.Read))
{
if(envPerm1 == null) continue;
Console.WriteLine("********************************************************\n");
envGen2.ResetIndex();
try
{
envPerm2= new EnvironmentPermission(PermissionState.None);
envPerm2.FromXml(envPerm1.ToXml());
Console.WriteLine("Result of ToFromXml = " +envPerm2.ToString() + "\n");
}
catch(Exception e)
{
Console.WriteLine("ToFromXml failed :" + envPerm1.ToString() + e);
continue;
}
}
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");
EnvironmentPermission envPerm1;
Console.WriteLine("Creating an EnvironmentPermission with AllAccess rights for 'TMP'");
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;
}
}
}
// This class generates EnvironmentPermission objects.
internal class EnvGenerator
{
private string[] myEnv =
{"windir", "TMP", "TEMP"};
private int envIndex = 0;
public EnvGenerator()
{
ResetIndex();
}
public void ResetIndex()
{
envIndex = 0;
}
// Create an EnvironmentPermission using a given EnvironmentPermissionAccess.
public bool CreateEnv(out EnvironmentPermission envPerm, out string env, EnvironmentPermissionAccess epa)
{
if(envIndex == myEnv.Length)
{
envPerm = new EnvironmentPermission(PermissionState.None);
env="";
envIndex++;
return true;
}
if(envIndex > myEnv.Length)
{
envPerm = null;
env = "";
return false;
}
env = myEnv[envIndex++];
try
{
envPerm = new EnvironmentPermission(epa, env);
return true;
}
catch(Exception e)
{
Console.WriteLine("Cannot create EnvironmentPermission : " + env +" "+e);
envPerm = new EnvironmentPermission(PermissionState.None);
env="";
return true;
}
}
} // End of EnvGenerator.
[C++]
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods
// of the EnvironmentPermission class.
#using <mscorlib.dll>
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Collections;
using namespace System::Runtime::InteropServices;
[assembly:CLSCompliant(true)];
// This class generates EnvironmentPermission objects.
private __gc class EnvGenerator {
private:
String* myEnv[];
int envIndex;
public:
EnvGenerator() {
String* temp0 [] = {S"windir", S"TMP", S"TEMP"};
myEnv = temp0;
ResetIndex();
}
void ResetIndex() {
envIndex = 0;
}
// Create an EnvironmentPermission using a given EnvironmentPermissionAccess.
public:
bool CreateEnv(
[Out] EnvironmentPermission** envPerm,
[Out] String** env, EnvironmentPermissionAccess epa) {
if (envIndex == myEnv->Length) {
*envPerm = new EnvironmentPermission(PermissionState::None);
*env=S"";
envIndex++;
return true;
}
if (envIndex > myEnv->Length) {
*envPerm = 0;
*env = S"";
return false;
}
*env = myEnv[envIndex++];
try {
*envPerm = new EnvironmentPermission(epa, *env);
return true;
} catch (Exception* e) {
Console::WriteLine(S"Cannot create EnvironmentPermission : {0}{1}", *env, e);
*envPerm = new EnvironmentPermission(PermissionState::None);
*env=S"";
return true;
}
}
}; // End of EnvGenerator.
public __gc class EnvironmentPermissionDemo {
// IsSubsetOf determines whether the current permission is a subset of the specified permission.
private:
bool IsSubsetOfDemo() {
bool returnValue = true;
String* env1, *env2;
EnvironmentPermission* envPerm1, *envPerm2;
EnvGenerator* envGen1 = new EnvGenerator();
EnvGenerator* envGen2 = new EnvGenerator();
envGen1->ResetIndex();
while(envGen1->CreateEnv(&envPerm1, &env1, EnvironmentPermissionAccess::Read)) {
if (envPerm1 == 0) continue;
Console::WriteLine(S"**********************************************************\n");
envGen2->ResetIndex();
while(envGen2->CreateEnv(&envPerm2, &env2, EnvironmentPermissionAccess::AllAccess)) {
String* firstPermission = env1->Equals(S"") || env1 == 0 ? S"null" : env1 ;
String* secondPermission = env2->Equals(S"") || env2 == 0 ? S"null" : env2;
if (envPerm2 == 0) continue;
try {
if (envPerm1->IsSubsetOf(envPerm2)) {
Console::WriteLine(S"{0} is a subset of {1}\n", firstPermission, secondPermission);
} else {
Console::WriteLine(S"{0} is not a subset of {1}\n", firstPermission, secondPermission);
}
} catch (Exception* e) {
Console::WriteLine(S"An exception was thrown for subset : {0}\n{1}\n{2}",
env1->Equals(S"") ? S"null" : env1,
env2->Equals(S"") ? S"null" : env2, e);
}
}
}
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;
String* env1, *env2;
EnvironmentPermission* envPerm1, *envPerm2;
IPermission* envIdPerm3;
EnvGenerator* envGen1 = new EnvGenerator();
EnvGenerator* envGen2 = new EnvGenerator();
envGen1->ResetIndex();
while(envGen1->CreateEnv(&envPerm1, &env1, EnvironmentPermissionAccess::Read)) {
if (env1 == 0) continue;
Console::WriteLine(S"**********************************************************\n");
envGen2->ResetIndex();
while(envGen2->CreateEnv(&envPerm2, &env2, EnvironmentPermissionAccess::Read)) {
try {
if (envPerm2 == 0) continue;
String* firstPermission = env1->Equals(S"") || env1 == 0 ? S"null" : env1 ;
String* secondPermission = env2->Equals(S"") || env2 == 0 ? S"null" : env2;
envIdPerm3 = dynamic_cast<EnvironmentPermission*>(envPerm1->Union(envPerm2));
envIdPerm3 = envPerm1->Union(envPerm2);
if (envIdPerm3 == 0) {
Console::WriteLine(S"The union of {0} and {1} is null.", firstPermission, secondPermission);
} else {
Console::WriteLine(S"The union of {0} and {1} = {2}", firstPermission, secondPermission,
(dynamic_cast<EnvironmentPermission*>(envIdPerm3))->GetPathList(EnvironmentPermissionAccess::Read));
}
} catch (Exception* e) {
Console::WriteLine(S"An exception was thrown for union {0}", e);
returnValue=false;
}
}
}
return returnValue;
}
// Intersect creates and returns a new permission that is the intersection of
// the current permission and the permission specified.
private:
bool IntersectDemo() {
bool returnValue = true;
String* env1, *env2;
EnvironmentPermission *envPerm1, *envPerm2, *envIdPerm3;
EnvGenerator* envGen1 = new EnvGenerator();
EnvGenerator* envGen2 = new EnvGenerator();
envGen1->ResetIndex();
while(envGen1->CreateEnv(&envPerm1, &env1, EnvironmentPermissionAccess::Read)) {
if (env1 == 0) continue;
Console::WriteLine(S"**********************************************************\n");
envGen2->ResetIndex();
while(envGen2->CreateEnv(&envPerm2, &env2, EnvironmentPermissionAccess::Read)) {
if (envPerm2 == 0) continue;
String* firstPermission = env1->Equals(S"") || env1 == 0 ? S"0" : env1 ;
String* secondPermission = env2->Equals(S"") || env2 == 0 ? S"0" : env2;
try {
envIdPerm3 = dynamic_cast<EnvironmentPermission*>(envPerm1->Intersect(envPerm2));
if (envIdPerm3 != 0 && envIdPerm3->GetPathList(EnvironmentPermissionAccess::Read) != 0) {
Console::WriteLine(S"The intersection of {0} and {1} = {2}", firstPermission, secondPermission,
(dynamic_cast<EnvironmentPermission*>(envIdPerm3))->GetPathList(EnvironmentPermissionAccess::Read));
} else {
Console::WriteLine(S"The intersection of {0} and {1} is null.", firstPermission, secondPermission);
}
} catch (Exception* e) {
Console::WriteLine(S"An exception was thrown for intersection : {0}", e);
returnValue=false;
}
}
}
return returnValue;
}
//Copy creates and returns an identical copy of the current permission.
private:
bool CopyDemo() {
bool returnValue = true;
String* env1;
EnvironmentPermission* envPerm1, *envPerm2;
EnvGenerator* envGen1 = new EnvGenerator();
EnvGenerator* envGen2 = new EnvGenerator();
envGen1->ResetIndex();
while(envGen1->CreateEnv(&envPerm1, &env1, EnvironmentPermissionAccess::Read)) {
if (env1 == 0) continue;
Console::WriteLine(S"**********************************************************\n");
envGen2->ResetIndex();
try {
envPerm2 = dynamic_cast<EnvironmentPermission*>(envPerm1->Copy());
if (envPerm2 != 0) {
Console::WriteLine(S"Result of copy = {0}\n", envPerm2);
} else {
Console::WriteLine(S"Result of copy is null. \n");
}
} catch (Exception* e) { {
if (env1->Equals(S"")) {
Console::WriteLine(S"The EnvironmentPermission is empty, copy failed.");
} else
Console::WriteLine(e);
}
continue;
}
}
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;
String* env1;
EnvironmentPermission* envPerm1, *envPerm2;
EnvGenerator* envGen1 = new EnvGenerator();
EnvGenerator* envGen2 = new EnvGenerator();
envGen1->ResetIndex();
while(envGen1->CreateEnv(&envPerm1, &env1, EnvironmentPermissionAccess::Read)) {
if (envPerm1 == 0) continue;
Console::WriteLine(S"********************************************************\n");
envGen2->ResetIndex();
try {
envPerm2 = new EnvironmentPermission(PermissionState::None);
envPerm2->FromXml(envPerm1->ToXml());
Console::WriteLine(S"Result of ToFromXml = {0}\n", envPerm2);
} catch (Exception* e) {
Console::WriteLine(S"ToFromXml failed : {0}{1}", envPerm1, e);
continue;
}
}
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(S"********************************************************\n");
EnvironmentPermission* envPerm1;
Console::WriteLine(S"Creating an EnvironmentPermission with AllAccess rights for 'TMP'");
envPerm1 = new EnvironmentPermission(EnvironmentPermissionAccess::AllAccess, S"TMP");
Console::WriteLine(S"Adding 'TEMP' to the write access list, and 'windir' to the read access list.");
envPerm1->AddPathList(EnvironmentPermissionAccess::Write, S"TEMP");
envPerm1->AddPathList(EnvironmentPermissionAccess::Read, S"windir");
Console::WriteLine(S"Read access list before SetPathList = {0}", envPerm1->GetPathList(EnvironmentPermissionAccess::Read));
Console::WriteLine(S"Setting read access to 'TMP'");
envPerm1->SetPathList(EnvironmentPermissionAccess::Read, S"TMP");
Console::WriteLine(S"Read access list after SetPathList = {0}", envPerm1->GetPathList(EnvironmentPermissionAccess::Read));
Console::WriteLine(S"Write access list = {0}", envPerm1->GetPathList(EnvironmentPermissionAccess::Write));
Console::WriteLine(S"Write access environment variables = {0}", envPerm1->GetPathList(EnvironmentPermissionAccess::AllAccess));
} catch (ArgumentException* e) {
// EnvironmentPermissionAccess::AllAccess cannot be used as a parameter for GetPathList.
Console::WriteLine(S"An ArgumentException occurred as a result of using AllAccess."
S" This property cannot be used as a parameter in GetPathList, because it represents more than one type of environment variable :\n{0}", e);
}
return true;
}
// Invoke all demos.
public:
bool RunDemo() {
bool ret=true;
bool retTmp;
// Call IsSubsetOf demo.
if (retTmp= IsSubsetOfDemo())Console::Out->WriteLine(S"IsSubset demo completed successfully.");
else Console::Out->WriteLine(S"IsSubset demo failed.");
ret=retTmp && ret;
// Call Union demo.
if (retTmp= UnionDemo())Console::Out->WriteLine(S"Union demo completed successfully.");
else Console::Out->WriteLine(S"Union demo failed.");
ret=retTmp && ret;
// Call Intersect demo.
if (retTmp= IntersectDemo())Console::Out->WriteLine(S"Intersect demo completed successfully.");
else Console::Out->WriteLine(S"Intersect demo failed.");
ret=retTmp && ret;
// Call Copy demo.
if (retTmp= CopyDemo())Console::Out->WriteLine(S"Copy demo completed successfully.");
else Console::Out->WriteLine(S"Copy demo failed.");
ret=retTmp && ret;
// Call ToFromXml demo.
if (retTmp= ToFromXmlDemo())Console::Out->WriteLine(S"ToFromXml demo completed successfully.");
else Console::Out->WriteLine(S"ToFromXml demo failed.");
ret=retTmp && ret;
// Call SetGetPathList demo.
if (retTmp= SetGetPathListDemo())Console::Out->WriteLine(S"SetGetPathList demo completed successfully.");
else Console::Out->WriteLine(S"SetGetPathList demo failed.");
ret=retTmp && ret;
return (ret);
}
};
// Test harness.
int main() {
try {
EnvironmentPermissionDemo* democase = new EnvironmentPermissionDemo();
bool ret = democase->RunDemo();
if (ret) {
Console::Out->WriteLine(S"EnvironmentPermission demo completed successfully.");
Console::Out->WriteLine(S"Press the Enter key to exit.");
Console::ReadLine();
System::Environment::ExitCode = 100;
} else {
Console::Out->WriteLine(S"EnvironmentPermission demo failed.");
Console::Out->WriteLine(S"Press the Enter key to exit.");
Console::ReadLine();
System::Environment::ExitCode = 101;
}
} catch (Exception* e) {
Console::Out->WriteLine(S"EnvironmentPermission demo failed.");
Console::WriteLine(e);
Console::Out->WriteLine(S"Press the Enter key to exit.");
Console::ReadLine();
System::Environment::ExitCode = 101;
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Security.Permissions
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
アセンブリ: Mscorlib (Mscorlib.dll 内)
参照
EnvironmentPermission メンバ | System.Security.Permissions 名前空間 | アクセス許可 | アクセス許可の要求 | EnvironmentPermissionAttribute | EnvironmentPermissionAccess