IsolatedStorageFilePermission-Klasse

Gibt die zulässige Verwendung eines privaten virtuellen Dateisystems an. Diese Klasse kann nicht vererbt werden.

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

Syntax

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class IsolatedStorageFilePermission
    Inherits IsolatedStoragePermission
'Usage
Dim instance As IsolatedStorageFilePermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class IsolatedStorageFilePermission : IsolatedStoragePermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class IsolatedStorageFilePermission sealed : public IsolatedStoragePermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class IsolatedStorageFilePermission extends IsolatedStoragePermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class IsolatedStorageFilePermission extends IsolatedStoragePermission

Hinweise

Die CLR (Common Language Runtime) steuert mit dieser Klasse den Zugriff auf den isolierten Speicherplatz.

Durch den isolierten Speicherplatz wird ein eindeutiger Speicherbereich erstellt, der von einer Anwendung oder Komponente verwendet werden kann. Die vollständige Isolation wird erreicht, indem über die Identität einer Anwendung ein eindeutiger Stamm für ein virtuelles Dateisystem bestimmt wird, auf das nur die betreffende Anwendung zugreifen kann. Jede Anwendung verfügt so über einen eigenen, automatisch zugewiesenen Dateibereich. Dieser Dateibereich ist vollständig von anderen Anwendungen isoliert und wird damit zu einem privaten Bereich der jeweiligen Anwendung.

Hinweis

Wenn Sie Assert, PermitOnly oder Deny verwenden, um Stapelmodifizierer für die Nutzung oder für ein Kontingent hinzuzufügen, hat dies keine Auswirkungen. Nutzung und Kontingente werden aus dem Beweis bestimmt, und für Anforderungen wird kein Stackwalk durchgeführt. Dadurch sind die oben genannten Vorgänge wirkungslos.

Beispiel

Im folgenden Beispiel wird die Verwendung von Membern der IsolatedStorageFilePermission-Klasse veranschaulicht.

Imports System
Imports System.Security
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Security.Permissions
Imports System.Diagnostics

Public Class Demo

    ' Main method.
    Public Sub Run()
        Try
            Copy_EqualsDemo()
            To_FromXmlDemo()
            GetTypeDemo()
            IntersectDemo()
            UnionDemo()
            ToStringDemo()
            Demand_DenyDemo()
            PermitOnlyDemo()
            UserQuota_UsageDemo()
            IsSubsetOfDemo()
            IsUnrestrictedDemo()
        Catch e As Exception

            Console.WriteLine((ControlChars.Cr + ControlChars.Lf + ControlChars.Lf + "The demo failed due to an unexpected exception" + ControlChars.Cr + ControlChars.Lf + e.ToString()))
        End Try
    End Sub 'Run

    ' IsUnrestrictedDemo demonstrates the IsolatedStorageFilePermission.IsUnrestricted method.
    ' IsUnrestrictedDemo displays a comment indicating whether the current permission is unrestricted.
    Private Sub IsUnrestrictedDemo()
        Console.WriteLine("IsUnrestricted demo.")
        Try
            Dim isfp As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
            isfp.Demand() ' The permission should be unrestricted.
            Console.WriteLine("Created and demanded the IsolatedStorageFilePermission(PermissionState.Unrestricted)permission.")
            If True <> isfp.IsUnrestricted() Then
                Console.WriteLine("IsUnrestricted demo failed. The IsUnrestricted property value should be 'true'")
            End If

            isfp = New IsolatedStorageFilePermission(PermissionState.None)
            isfp.Demand() ' The permission should not be unrestricted.
            Console.WriteLine("Created and demanded the IsolatedStorageFilePermission(PermissionState.None)permission.")
            If False <> isfp.IsUnrestricted() Then
                Console.WriteLine("IsUnrestricted demo failed. The IsUnrestricted property value should be 'false'")
            End If
        Catch e As Exception
            Console.WriteLine(("Unexpected exception: " + e.Message))
        End Try
    End Sub 'IsUnrestrictedDemo

    ' Copy_EqualsDemo demonstrates the Copy and Equals methods.
    ' Copy creates and returns an identical copy of the current permission.
    ' Equals compares two permissions and determines whether they are equal.
    Private Sub Copy_EqualsDemo()
        Console.WriteLine(ControlChars.Lf + "Copy_Equals demo.")
        Console.WriteLine("Creating the first IsolatedStorageFilePermission(PermissionState.Unrestricted) permission.")
        Dim first As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Console.WriteLine(("Hashcode for the first permission : " + first.GetHashCode().ToString()))
        Console.WriteLine("Copying the first permission.")
        Dim copyOfFirst As IsolatedStorageFilePermission = CType(first.Copy(), IsolatedStorageFilePermission)
        Console.WriteLine(("Hashcode for copy of the first permission: " + copyOfFirst.GetHashCode().ToString()))
        If False = copyOfFirst.Equals(first) Then ' This should be false, because the object references are different.
            Console.WriteLine(("The copy and the first permission should not be equal because they have " + ControlChars.Lf + ControlChars.Tab + "different object references."))
        Else
            Console.WriteLine(("Error: the copy and the first permission should not be equal because they have " + ControlChars.Lf + ControlChars.Tab + "different object references."))
        End If

        Console.WriteLine(("Does a comparison of copy of first permission to itself test equal??: " + IIf(copyOfFirst.Equals(copyOfFirst), "true", "false")))
        If False = copyOfFirst.Equals(copyOfFirst) Then
            Console.WriteLine("Copy_Equals demo failed because Equals returns false on two object.Equals(itself).")
        End If

        Console.WriteLine(("Does a comparison of first permission to itself test equal?: " + IIf(first.Equals(first), "true", "false")))
        If False = first.Equals(first) Then
            Console.WriteLine("Copy_Equals demo failed, because Equals returns false on two object.Equals(itself).")
        End If
        Console.WriteLine("Creating a second permission with PermissionState.None.")
        Dim second As New IsolatedStorageFilePermission(PermissionState.None)

        Console.WriteLine(("Does the first permission equal the second permission?: " + IIf(first.Equals(second), "true", "false")))
        If True = first.Equals(second) Then
            Console.WriteLine("Copy_Equals demo failed because Equals returns false on two object.Equals(itself).")
        End If
    End Sub 'Copy_EqualsDemo

    ' To_FromXmlDemo demonstrates ToXml and FromXml.
    ' ToXml creates an XML encoding of the permission and its current state.
    ' FromXml reconstructs a permission with a specified state from an XML encoding.
    Private Sub To_FromXmlDemo()
        Console.WriteLine(ControlChars.Lf + "To_FromXML demo.")
        Dim firstPermission As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Console.WriteLine(("First permission: " + firstPermission.ToString()))
        Dim se As SecurityElement = firstPermission.ToXml()
        Dim secondPermission As New IsolatedStorageFilePermission(PermissionState.None)
        Console.WriteLine(("Second permission: " + secondPermission.ToString()))
        secondPermission.FromXml(se)
        Dim thirdPermission As IsolatedStorageFilePermission = CType(secondPermission.Intersect(firstPermission), IsolatedStorageFilePermission)
        Console.WriteLine(("Intersection of first permission and second permission: " + thirdPermission.ToString()))

        Try
            If False = thirdPermission.IsUnrestricted() Then
                Console.WriteLine("To_FromXml demo failed: The ToXml, FromXml roundtrip did not succeed.")
            End If
        Catch e As Exception
            Console.WriteLine("To_FromXml demo failed: An exception was thrown when checking the intersection")
            Console.WriteLine("of the permissions after the XML roundtrip.")
            Console.WriteLine(e.ToString())

            Console.WriteLine(e.ToString())
        End Try
    End Sub 'To_FromXmlDemo

    ' GetType demonstrates the GetType method.
    ' This method returns the type of the current instance.
    Private Sub GetTypeDemo()
        Console.WriteLine(ControlChars.Lf + "GetType demo.")
        Dim isfp As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Dim isfp1 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)

        If 0 <> isfp.GetType().ToString().CompareTo("System.Security.Permissions.IsolatedStorageFilePermission") Then
            Console.WriteLine(("GetType returned the wrong value: " + isfp.GetType().ToString()))
        Else
            Console.WriteLine(("GetType returned: " + isfp.GetType().ToString()))
        End If
    End Sub 'GetTypeDemo

    ' IntersectDemo demonstrates the Intersect method.
    ' CIntersect creates and returns a permission that is the intersection of the current permission
    ' and the specified permission.
    ' Note  The intersection of two PermissionState.None permissions *IS* null. 
    Private Sub IntersectDemo()
        Console.WriteLine(ControlChars.Lf + "Intersect demo.")
        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Dim isfp1 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Dim isfp2 As New IsolatedStorageFilePermission(PermissionState.None)
        Dim isfp3 As New IsolatedStorageFilePermission(PermissionState.None)

        Dim t As IsolatedStorageFilePermission = CType(isfp0.Intersect(isfp1), IsolatedStorageFilePermission)
        If True = t.IsUnrestricted() Then
            Console.WriteLine("The intersection of two PermissionState.Unrestricted permissions is unrestricted.")
        Else
            Console.WriteLine(("Intersect demo failed because the intersection of two unrestricted permissions " + ControlChars.Lf + ControlChars.Tab + "should be unrestricted."))
        End If

        t = CType(isfp0.Intersect(isfp2), IsolatedStorageFilePermission)
        If False = t.IsUnrestricted() Then
            Console.WriteLine(("The intersection of a PermissionState.Unrestricted and " + ControlChars.Lf + ControlChars.Tab + "PermissionState.None permission is not unrestricted."))
        Else
            Console.WriteLine(("Intersect demo failed because the intersection of a unrestricted and " + ControlChars.Lf + ControlChars.Tab + "none permission should be none."))
        End If


        ' The permission set that comes back from this intersection is null.
        t = CType(isfp2.Intersect(isfp3), IsolatedStorageFilePermission)
        If Nothing Is t Then
            Console.WriteLine("The intersection of two PermissionState.None permissions is null.")
        Else
            Console.WriteLine(("Intersect failed because the intersection of two PermissionState.None permissions " + ControlChars.Lf + ControlChars.Tab + "should be null."))
        End If
    End Sub 'IntersectDemo

    ' UnionDemo demonstrates the Union method.
    ' Union creates a permission that is the union of the current permission and the specified permission.
    ' Note: The union of two PermissionState.None permissions is *NOT* null.
    Private Sub UnionDemo()
        Console.WriteLine(ControlChars.Lf + "Union demo.")
        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Dim isfp1 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Dim isfp2 As New IsolatedStorageFilePermission(PermissionState.None)
        Dim isfp3 As New IsolatedStorageFilePermission(PermissionState.None)

        Dim t As IsolatedStorageFilePermission = CType(isfp0.Union(isfp1), IsolatedStorageFilePermission)
        If True = t.IsUnrestricted() Then
            Console.WriteLine("The union of two PermissionState.Unrestricted permissions is unrestricted.")
        Else
            Console.WriteLine(("Union demo failed because the union of two PermissionState.Unrestricted permissions " + ControlChars.Lf + ControlChars.Tab + "should be unrestricted"))
        End If

        t = CType(isfp0.Union(isfp2), IsolatedStorageFilePermission)
        If True = t.IsUnrestricted() Then
            Console.WriteLine(("The union of a PermissionState.Unrestricted and a PermissionState.None " + ControlChars.Lf + ControlChars.Tab + "permission is unrestricted."))
        Else
            Console.WriteLine(("Union demo failed because the union of a PermissionState.Unrestricted and a " + ControlChars.Lf + ControlChars.Tab + "PermissionState.None permission should be unrestricted."))
        End If


        t = CType(isfp2.Union(isfp3), IsolatedStorageFilePermission)
        If Nothing Is t Then
            Console.WriteLine("Union failed because the union of two PermissionState.None permissions should not be null.")
            Console.WriteLine(t.ToString())
        Else
            Console.WriteLine("The union of two PermissionState.None permissions is none.")
        End If
    End Sub 'UnionDemo

    ' ToStringDemo demonstrates the ToString method.
    ' ToString creates and returns a string representation of the current permission object.
    Private Sub ToStringDemo()
        Console.WriteLine(ControlChars.Lf + "ToString demo.")
        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Dim PermString As [String] = isfp0.ToString()

        Dim PermStringParts As [String]() = {"class=", "mscorlib", "Version=", "Culture=", "PublicKeyToken=", "version=", "Unrestricted="}


        Dim part As [String]
        For Each part In PermStringParts
            If PermString.IndexOf(part) >= 0 Then
                Console.WriteLine((part + " found in the string."))
            Else
                Console.WriteLine("ToString demo failed because [{0}] was not found.", part)
            End If
        Next part
    End Sub 'ToStringDemo

    ' Demand_Deny demonstrates Demand and Deny.
    ' Demand forces a SecurityException at run time if all callers higher in the call stack 
    ' have not been granted the permission specified by the current instance.
    ' Deny prevents callers higher in the call stack from using the code that calls this method to 
    ' access the resource specified by the current instance.
    Private Sub Demand_DenyDemo()
        Console.WriteLine(ControlChars.Lf + "Demand_Deny demo.")
        Console.WriteLine("Denying a permission with PermissionState.Unrestricted.")
        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        isfp0.Deny()
        Try
            Console.WriteLine("Denying permission a second time.")
            isfp0.Deny()
        Catch e As SecurityException
            Console.WriteLine("SecurityException was thrown as expected.")
        Catch e As Exception
            Console.WriteLine(("Demand_Deny demo failed because an unexpected exception was thrown" + ControlChars.Lf + ControlChars.Tab + "when making a second call to Deny()" + ControlChars.Cr + ControlChars.Lf + e.ToString()))
        End Try
        Console.WriteLine("Reverting the deny.")
        IsolatedStorageFilePermission.RevertDeny()

        If 0 <> DoDemand() Then
            Console.WriteLine("Demand_Deny demo failed because an unexpected exception was thrown during a demand.")
        End If

        isfp0.Deny()
        If 1 <> DoDemand() Then
            Console.WriteLine("Demand_Deny demo failed because the expected SecurityException was not thrown during a demand.")
        End If

        IsolatedStorageFilePermission.RevertDeny()
        If 0 <> DoDemand() Then
            Console.WriteLine("Demand_Deny demo failed because an unexpected exception was thrown during a demand.")
        End If
    End Sub 'Demand_DenyDemo

    ' PermitOnly demonstrates the PermitOnly method.
    ' PermitOnly prevents callers higher in the call stack from using the code that calls this method to 
    ' access all resources except for the resource specified by the current instance.
    Private Sub PermitOnlyDemo()
        Console.WriteLine(ControlChars.Lf + "PermitOnly demo.")

        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        isfp0.Demand()
        isfp0.PermitOnly()
        Try
            isfp0.PermitOnly()
        Catch e As SecurityException
            Console.WriteLine("The second call to PermitOnly caused an exception as expected.")
        Catch e As Exception
            Console.WriteLine(("PermitOnly demo failed because an unexpected exception was thrown" + ControlChars.Lf + ControlChars.Tab + "when making a second call to PermitOnly()" + ControlChars.Cr + ControlChars.Lf + e.ToString()))
        End Try

        IsolatedStorageFilePermission.RevertPermitOnly()
        If 0 <> DemandUI() Then
            Console.WriteLine("PermitOnly demo failed because an exception was thrown during a demand for UI permissions.")
            Console.WriteLine("We weren't expecting this.")
        End If

        isfp0.PermitOnly()
        If 1 <> DemandUI() Then
            Console.WriteLine(("PermitOnly demo failed because a SecurityException was not thrown, " + ControlChars.Lf + ControlChars.Tab + "during a demand for UI permissions.  A SecurityException was expected."))
        End If


        IsolatedStorageFilePermission.RevertPermitOnly()
        If 0 <> DoDemand() Then
            Console.WriteLine("PermitOnly failed because an exception was thrown during a demand for UI permissions.")
            Console.WriteLine("We weren't expecting this.")
        End If
    End Sub 'PermitOnlyDemo

    ' UserQuota_UsageDemo demonstrates UserQuota and UsageAllowed properties.
    ' UserQuota gets or sets the quota on the overall size of each user's total store.  
    ' This property is inherited from IsolatedStoragePermission.UserQuota.
    ' UsageAllowed gets or sets the type of isolated storage containment allowed.  
    ' This property is inherited from IsolatedStoragePermission.UsageAllowed.
    ' Note  You can set UsageAllowed to None, but you cannot set UserQuota to 0.
    ' Also note that trying to set UsageAllowed or UserQuota to values that are currently set
    ' will throw an exception.  
    <IsolatedStorageFilePermissionAttribute(SecurityAction.Deny, _
    UsageAllowed:=IsolatedStorageContainment.DomainIsolationByRoamingUser, UserQuota:=100)> _
   Private Sub UserQuota_UsageDemo()
        Console.WriteLine(ControlChars.Lf + "UserQuota_Usage demo.")
        Dim returnValue As Boolean = False

        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.None)

        Console.WriteLine("Checking UsageAllowed for IsolatedStorageContainment.None")
        returnValue = UsageAllowed(isfp0, IsolatedStorageContainment.None, False)
        If Not returnValue Then
            Console.WriteLine("IsolatedStorageContainment.None demo failed")
        End If

        Console.WriteLine("Checking UsageAllowed for IsolatedStorageContainment.DomainIsolationByRoamingUser.")
        returnValue = UsageAllowed(isfp0, IsolatedStorageContainment.DomainIsolationByRoamingUser, True)
        If Not returnValue Then
            Console.WriteLine("IsolatedStorageContainment.DomainIsolationByRoamingUser failed")
        End If

        Console.WriteLine("Checking UsageAllowed for IsolatedStorageContainment.AssemblyIsolationByUser.")
        returnValue = UsageAllowed(isfp0, IsolatedStorageContainment.AssemblyIsolationByUser, True)
        If Not returnValue Then
            Console.WriteLine("IsolatedStorageContainment.AssemblyIsolationByUse failed")
        End If

        Console.WriteLine("Requesting UserQuota of 0.")
        returnValue = UserQuota(isfp0, 0, True)
        If Not returnValue Then
            Console.WriteLine("UserQuota 0 failed")
        End If

        Console.WriteLine("Requesting UserQuota of 100.")
        returnValue = UserQuota(isfp0, 100, True)
        If Not returnValue Then
            Console.WriteLine("UserQuota 100 failed")
        End If

        Console.WriteLine("Requesting UserQuota of -2147483648.")
        returnValue = UserQuota(isfp0, -2147483648, True)

        If Not returnValue Then
            Console.WriteLine("UserQuota -214748364 failed")
        End If
    End Sub 'UserQuota_UsageDemo

    ' IsSubsetOfDemo demonstrates the IsSubsetOf method.
    ' IsSubsetOf determines whether the current permission is a subset of the specified permission.
    Private Sub IsSubsetOfDemo()
        Console.WriteLine(ControlChars.Lf + "IsSubsetOf demo.")

        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.None)
        Dim isfp1 As New IsolatedStorageFilePermission(PermissionState.None)
        Dim isfp2 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Dim isfp3 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)

        If isfp0.IsSubsetOf(isfp1) Then
            Console.WriteLine("PermissionState.None is a subset of PermissionState.None.")
        Else
            Console.WriteLine("IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.None")
        End If

        If isfp0.IsSubsetOf(isfp2) Then
            Console.WriteLine("PermissionState.None is a subset of PermissionState.Unrestricted.")
        Else
            Console.WriteLine("IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.Unrestricted")
        End If

        If Not isfp2.IsSubsetOf(isfp0) Then
            Console.WriteLine("PermissionState.Unrestricted is not a subset of PermissionState.None.")
        Else
            Console.WriteLine(("IsSubsetOfDemo failed: PermissionState.Unrestricted should not be a subset " + ControlChars.Lf + ControlChars.Tab + "of PermissionState.None"))
        End If

        If isfp2.IsSubsetOf(isfp3) Then
            Console.WriteLine("PermissionState.Unrestricted is a subset of PermissionState.Unrestricted.")
        Else
            Console.WriteLine(("IsSubsetOfDemo failed: Permissionstate.Unrestricted should be a subset of " + ControlChars.Lf + ControlChars.Tab + "PermissionState.Unrestricted"))
        End If
    End Sub 'IsSubsetOfDemo

    ' **********************************************************************
    ' The following methods are simple helper functions.
    ' **********************************************************************
    ' **********************************************************************
    ' UserQuota
    ' Sets UserQuota to the value that was passed in.
    '
    Private Function UserQuota(ByVal isfp As IsolatedStorageFilePermission, ByVal user_quota As Integer, ByVal exception_expected As Boolean) As Boolean
        Dim ExceptionCaught As Boolean = False
        isfp.UserQuota = user_quota

        Try
            isfp.Demand()
        Catch e As Exception
            ExceptionCaught = True
            If Not exception_expected Then
                Console.WriteLine("An unexpected exception was thrown when setting UserQuota value [{0}]", user_quota.ToString())
                Console.WriteLine(("Exception: " + ControlChars.Cr + ControlChars.Lf + e.ToString()))
            End If
        End Try

        If Not ExceptionCaught AndAlso exception_expected Then
            Console.WriteLine("An expected exception not thrown when setting UserQuota value [{0}]", user_quota.ToString())
        End If

        Return True
    End Function 'UserQuota


    ' **********************************************************************
    ' UsageAllowed
    ' This method sets UsageAllowed to the value that was passed in.
    '
    Private Function UsageAllowed(ByVal isfp As IsolatedStorageFilePermission, ByVal isc As IsolatedStorageContainment, ByVal exception_expected As Boolean) As Boolean
        Dim ExceptionCaught As Boolean = False
        isfp.UsageAllowed = isc
        Try
            isfp.Demand()
        Catch e As Exception
            ExceptionCaught = True
            If Not exception_expected Then

                Console.WriteLine("An unexpected exception was thrown when setting IsolatedStorageContainment value [{0}]", isc.ToString())
                Console.WriteLine(("Exception: " + ControlChars.Cr + ControlChars.Lf + e.ToString()))
            End If
        End Try

        If Not ExceptionCaught AndAlso exception_expected Then
            Console.WriteLine("An expected exception was not thrown when setting IsolatedStorageContainment value [{0}]", isc.ToString())
        End If

        Return True
    End Function 'UsageAllowed



    ' **********************************************************************
    ' DoDemand()
    ' This method demands an IsolatedStorageFilePermission.
    Private Function DoDemand() As Integer
        Dim ReturnValue As Integer = 0
        Dim isfp0 As New IsolatedStorageFilePermission(PermissionState.Unrestricted)
        Try
            isfp0.Demand()
        Catch e As SecurityException
            ReturnValue = 1
        Catch e As Exception
            Console.WriteLine(("An unexpected exception was thrown in DoDemand" + ControlChars.Cr + ControlChars.Lf + e.ToString()))
            ReturnValue = -1
        End Try

        Return ReturnValue
    End Function 'DoDemand


    ' **********************************************************************
    ' DemandUI()
    ' This method demands a UIPermission
    ' Returns:
    '    0 if the demand was successful.
    '    1 if a SecurityException was thrown.
    '    -1 if an unexpected exception was thrown.
    '
    Private Function DemandUI() As Integer
        Dim ReturnValue As Integer = 0
        Dim MyPermission As New UIPermission(PermissionState.Unrestricted)
        Try
            MyPermission.Demand()
        Catch e As SecurityException
            ReturnValue = 1
        Catch e As Exception
            Console.WriteLine(("An unexpected exception was thrown in DemandUI" + ControlChars.Cr + ControlChars.Lf + e.ToString()))
        End Try

        Return ReturnValue
    End Function 'DemandUI

    Public Overloads Shared Function Main(ByVal args() As [String]) As Integer
        Dim MyDemo As New Demo

        MyDemo.Run()
        Return 0
    End Function 'Main
End Class 'Demo
using System;
using System.Security;
using System.IO;
using System.IO.IsolatedStorage;
using System.Security.Permissions;
using System.Diagnostics;

public class Demo
{
    // Main method.
    public void Run()
    {
        try
        {
            Copy_EqualsDemo();
            To_FromXmlDemo();
            GetTypeDemo();
            IntersectDemo();
            UnionDemo();
            ToStringDemo();
            Demand_DenyDemo();
            PermitOnlyDemo();
            UserQuota_UsageDemo();
            IsSubsetOfDemo();
            IsUnrestrictedDemo();
        }
        catch (Exception e)
        {

            Console.WriteLine("\r\n\nThe demo failed due to an unexpected exception\r\n"+e.ToString());
        }
    }




    // IsUnrestrictedDemo demonstrates the IsolatedStorageFilePermission.IsUnrestricted method.
    // IsUnrestrictedDemo displays a comment indicating whether the current permission is unrestricted.
    private void IsUnrestrictedDemo()
    {
        Console.WriteLine("IsUnrestricted demo.");
        try
        {
            IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
            isfp.Demand(); // The permission should be unrestricted.
            Console.WriteLine
                ("Created and demanded the IsolatedStorageFilePermission(PermissionState.Unrestricted)permission." );
            if ( true != isfp.IsUnrestricted() )
            {
                Console.WriteLine
                    ("IsUnrestricted demo failed. The IsUnrestricted property value should be 'true'");
            }

            isfp = new IsolatedStorageFilePermission(PermissionState.None);
            isfp.Demand(); // The permission should not be unrestricted.
            Console.WriteLine
                ("Created and demanded the IsolatedStorageFilePermission(PermissionState.None)permission.");
            if ( false != isfp.IsUnrestricted() )
            {
                Console.WriteLine
                    ("IsUnrestricted demo failed. The IsUnrestricted property value should be 'false'");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine
                ("Unexpected exception: " + e.Message);
        }

    }

    // Copy_EqualsDemo demonstrates the Copy and Equals methods.
    // Copy creates and returns an identical copy of the current permission.
    // Equals compares two permissions and determines whether they are equal.
    private void Copy_EqualsDemo()
    {
        Console.WriteLine
            ("\nCopy_Equals demo.");
        Console.WriteLine
            ("Creating the first IsolatedStorageFilePermission(PermissionState.Unrestricted) permission.");
        IsolatedStorageFilePermission first = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        Console.WriteLine
            ("Hashcode for the first permission : " + first.GetHashCode().ToString());
        Console.WriteLine
            ("Copying the first permission.");
        IsolatedStorageFilePermission copyOfFirst = (IsolatedStorageFilePermission)first.Copy();
        Console.WriteLine
            ("Hashcode for copy of the first permission: " + copyOfFirst.GetHashCode().ToString());
        if (false == copyOfFirst.Equals(first)) // This should be false, because the object references are different.
        {
            Console.WriteLine
                ("The copy and the first permission should not be equal because they have " +
                "\n\tdifferent object references.");
        }
        else
        {
            Console.WriteLine
                ("Error: the copy and the first permission should not be equal because they have " +
                "\n\tdifferent object references.");
        }

        Console.WriteLine
            ("Does a comparison of copy of first permission to itself test equal??: "
            + (copyOfFirst.Equals(copyOfFirst)? "true": "false"));
        if (false == copyOfFirst.Equals(copyOfFirst))
        {
            Console.WriteLine
                ("Copy_Equals demo failed because Equals returns false on two object.Equals(itself).");
        }

        Console.WriteLine
            ("Does a comparison of first permission to itself test equal?: " + (first.Equals(first)? "true": "false"));
        if (false == first.Equals(first))
        {
            Console.WriteLine
                ("Copy_Equals demo failed, because Equals returns false on two object.Equals(itself).");
        }
        Console.WriteLine("Creating a second permission with PermissionState.None.");
        IsolatedStorageFilePermission second = new IsolatedStorageFilePermission(PermissionState.None);

        Console.WriteLine
            ("Does the first permission equal the second permission?: " + (first.Equals(second)? "true": "false"));
        if (true == first.Equals(second))
        {
            Console.WriteLine
                ("Copy_Equals demo failed because Equals returns false on two object.Equals(itself).");
        }
    }

    // To_FromXmlDemo demonstrates ToXml and FromXml.
    // ToXml creates an XML encoding of the permission and its current state.
    // FromXml reconstructs a permission with a specified state from an XML encoding.
    private void To_FromXmlDemo()
    {
        Console.WriteLine
            ("\nTo_FromXML demo.");
        IsolatedStorageFilePermission firstPermission = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        Console.WriteLine
            ("First permission: " + firstPermission.ToString());
        SecurityElement se = firstPermission.ToXml();
        IsolatedStorageFilePermission secondPermission = new IsolatedStorageFilePermission(PermissionState.None);
        Console.WriteLine
            ("Second permission: " + secondPermission.ToString());
        secondPermission.FromXml(se);
        IsolatedStorageFilePermission thirdPermission = (IsolatedStorageFilePermission)secondPermission.Intersect(firstPermission);
        Console.WriteLine
            ("Intersection of first permission and second permission: " + thirdPermission.ToString());

        try
        {
            if (false == thirdPermission.IsUnrestricted())
            {
                Console.WriteLine
                    ("To_FromXml demo failed: The ToXml, FromXml roundtrip did not succeed.");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("To_FromXml demo failed: An exception was thrown when checking the intersection");
            Console.WriteLine("of the permissions after the XML roundtrip.");
            Console.WriteLine(e.ToString());

            Console.WriteLine(e.ToString());
        }

    }

    // GetType demonstrates the GetType method.
    // This method returns the type of the current instance.
    private void GetTypeDemo()
    {
        Console.WriteLine("\nGetType demo.");
        IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp1 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);

        if (0 != isfp.GetType().ToString().CompareTo("System.Security.Permissions.IsolatedStorageFilePermission"))
        {
            Console.WriteLine
                ("GetType returned the wrong value: " + isfp.GetType().ToString());
        }
        else
        {
            Console.WriteLine
                ("GetType returned: " + isfp.GetType().ToString());
        }
    }
    // IntersectDemo demonstrates the Intersect method.
    // CIntersect creates and returns a permission that is the intersection of the current permission
    // and the specified permission.
    // Note The intersection of two PermissionState.None permissions *IS* null.
    private void IntersectDemo()
    {
        Console.WriteLine("\nIntersect demo.");
        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp1 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp2 = new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp3 = new IsolatedStorageFilePermission(PermissionState.None);

        IsolatedStorageFilePermission t = (IsolatedStorageFilePermission)isfp0.Intersect(isfp1);
        if (true == t.IsUnrestricted())
        {
            Console.WriteLine
                ("The intersection of two PermissionState.Unrestricted permissions is unrestricted.");
        }
        else
        {
            Console.WriteLine
                ("Intersect demo failed because the intersection of two unrestricted permissions " +
                "\n\tshould be unrestricted.");
        }

        t = (IsolatedStorageFilePermission)isfp0.Intersect(isfp2);
        if (false == t.IsUnrestricted())
        {
            Console.WriteLine
                ("The intersection of a PermissionState.Unrestricted and " +
                "\n\tPermissionState.None permission is not unrestricted.");
        }
        else
        {
            Console.WriteLine
                ("Intersect demo failed because the intersection of a unrestricted and " +
                "\n\tnone permission should be none.");
        }


        // The permission set that comes back from this intersection is null.
        t = (IsolatedStorageFilePermission)isfp2.Intersect(isfp3);
        if (null == t)
        {
            Console.WriteLine
                ("The intersection of two PermissionState.None permissions is null.");
        }
        else
        {
            Console.WriteLine
                ("Intersect failed because the intersection of two PermissionState.None permissions " +
                "\n\tshould be null.");
        }

    }

    // UnionDemo demonstrates the Union method.
    // Union creates a permission that is the union of the current permission and the specified permission.
    // Note: The union of two PermissionState.None permissions is *NOT* null.
    private void UnionDemo()
    {
        Console.WriteLine("\nUnion demo.");
        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp1 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp2 = new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp3 = new IsolatedStorageFilePermission(PermissionState.None);

        IsolatedStorageFilePermission t = (IsolatedStorageFilePermission)isfp0.Union(isfp1);
        if (true == t.IsUnrestricted())
        {
            Console.WriteLine("The union of two PermissionState.Unrestricted permissions is unrestricted.");
        }
        else
        {
            Console.WriteLine
                ("Union demo failed because the union of two PermissionState.Unrestricted permissions " +
                "\n\tshould be unrestricted");
        }

        t = (IsolatedStorageFilePermission)isfp0.Union(isfp2);
        if (true == t.IsUnrestricted())
        {
            Console.WriteLine
                ("The union of a PermissionState.Unrestricted and a PermissionState.None " +
                "\n\tpermission is unrestricted.");
        }
        else
        {
            Console.WriteLine
                ("Union demo failed because the union of a PermissionState.Unrestricted and a " +
                "\n\tPermissionState.None permission should be unrestricted.");
        }


        t = (IsolatedStorageFilePermission)isfp2.Union(isfp3);
        if (null != t)
        {
            Console.WriteLine("The union of two PermissionState.None permissions is none.");
        }
        else
        {
            Console.WriteLine
                ("Union failed because the union of two PermissionState.None permissions should not be null.");
            Console.WriteLine(t.ToString());
        }
    }

    // ToStringDemo demonstrates the ToString method.
    // ToString creates and returns a string representation of the current permission object.
    private void ToStringDemo()
    {
        Console.WriteLine("\nToString demo.");
        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        String PermString = isfp0.ToString();

        String[] PermStringParts = {"class=" ,"mscorlib" ,"Version=" ,"Culture=" ,"PublicKeyToken="
                                       ,"version=" ,"Unrestricted="};


        foreach (String part in PermStringParts)
        {
            if (PermString.IndexOf(part) >= 0)
            {
                Console.WriteLine(part + " found in the string.");
            }
            else
            {
                Console.WriteLine("ToString demo failed because [{0}] was not found.", part);
            }
        }

    }

    // Demand_Deny demonstrates Demand and Deny.
    // Demand forces a SecurityException at run time if all callers higher in the call stack
    // have not been granted the permission specified by the current instance.
    // Deny prevents callers higher in the call stack from using the code that calls this method to
    // access the resource specified by the current instance.
    private void Demand_DenyDemo()
    {
        Console.WriteLine("\nDemand_Deny demo.");
        Console.WriteLine("Denying a permission with PermissionState.Unrestricted.");
        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        isfp0.Deny();
        try
        {
            Console.WriteLine("Denying permission a second time.");
            isfp0.Deny();
        }
        catch (SecurityException)
        {
            Console.WriteLine("SecurityException was thrown as expected.");
        }
        catch (Exception e)
        {
            Console.WriteLine
                ("Demand_Deny demo failed because an unexpected exception was thrown" +
                "\n\twhen making a second call to Deny()\r\n" + e.ToString());
        }
        Console.WriteLine("Reverting the deny.");
        IsolatedStorageFilePermission.RevertDeny();

        if (0 != DoDemand())
        {
            Console.WriteLine
                ("Demand_Deny demo failed because an unexpected exception was thrown during a demand.");
        }

        isfp0.Deny();
        if (1 != DoDemand())
        {
            Console.WriteLine
                ("Demand_Deny demo failed because the expected SecurityException was not thrown during a demand.");
        }

        IsolatedStorageFilePermission.RevertDeny();
        if (0 != DoDemand())
        {
            Console.WriteLine
                ("Demand_Deny demo failed because an unexpected exception was thrown during a demand.");
        }
    }

    // PermitOnly demonstrates the PermitOnly method.
    // PermitOnly prevents callers higher in the call stack from using the code that calls this method to
    // access all resources except for the resource specified by the current instance.
    private void PermitOnlyDemo()
    {
        Console.WriteLine("\nPermitOnly demo.");

        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        isfp0.Demand();
        isfp0.PermitOnly();
        try
        {
            isfp0.PermitOnly();
        }
        catch (SecurityException)
        {
            Console.WriteLine
                ("The second call to PermitOnly caused an exception as expected.");
        }
        catch (Exception e)
        {
            Console.WriteLine
                ("PermitOnly demo failed because an unexpected exception was thrown" +
                "\n\twhen making a second call to PermitOnly()\r\n" + e.ToString());
        }

        IsolatedStorageFilePermission.RevertPermitOnly();
        if (0 != DemandUI())
        {
            Console.WriteLine
                ("PermitOnly demo failed because an exception was thrown during a demand for UI permissions.");
            Console.WriteLine
                ("We weren't expecting this.");
        }

        isfp0.PermitOnly();
        if (1 != DemandUI())
        {
            Console.WriteLine
                ("PermitOnly demo failed because a SecurityException was not thrown, " +
                "\n\tduring a demand for UI permissions. A SecurityException was expected.");

        }

        IsolatedStorageFilePermission.RevertPermitOnly();
        if (0 != DoDemand())
        {
            Console.WriteLine
                ("PermitOnly failed because an exception was thrown during a demand for UI permissions.");
            Console.WriteLine("We weren't expecting this.");
        }

    }


    // UserQuota_UsageDemo demonstrates UserQuota and UsageAllowed properties.
    // UserQuota gets or sets the quota on the overall size of each user's total store.
    // This property is inherited from IsolatedStoragePermission.UserQuota.
    // UsageAllowed gets or sets the type of isolated storage containment allowed.
    // This property is inherited from IsolatedStoragePermission.UsageAllowed.
    // Note You can set UsageAllowed to None, but you cannot set UserQuota to 0.
    // Also note that trying to set UsageAllowed or UserQuota to values that are currently set
    // will throw an exception.
    [method:IsolatedStorageFilePermissionAttribute(SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser, UserQuota=100)]
    private void UserQuota_UsageDemo()
    {
        Console.WriteLine
            ("\nUserQuota_Usage demo.");
        bool returnValue = false;

        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.None);

        Console.WriteLine
            ("Checking UsageAllowed for IsolatedStorageContainment.None");
        returnValue = UsageAllowed(isfp0, IsolatedStorageContainment.None, false);
        if (!returnValue)
            Console.WriteLine
                ("IsolatedStorageContainment.None demo failed");

        Console.WriteLine
            ("Checking UsageAllowed for IsolatedStorageContainment.DomainIsolationByRoamingUser.");
        returnValue = UsageAllowed(isfp0, IsolatedStorageContainment.DomainIsolationByRoamingUser, true);
        if (!returnValue)
            Console.WriteLine
                ("IsolatedStorageContainment.DomainIsolationByRoamingUser failed");

        Console.WriteLine
            ("Checking UsageAllowed for IsolatedStorageContainment.AssemblyIsolationByUser.");
        returnValue = UsageAllowed(isfp0, IsolatedStorageContainment.AssemblyIsolationByUser, true);
        if (!returnValue)
            Console.WriteLine
                ("IsolatedStorageContainment.AssemblyIsolationByUse failed");

        Console.WriteLine
            ("Requesting UserQuota of 0.");
        returnValue = UserQuota(isfp0, 0, true);
        if (!returnValue)
            Console.WriteLine
                ("UserQuota 0 failed");

        Console.WriteLine
            ("Requesting UserQuota of 100.");
        returnValue = UserQuota(isfp0, 100, true);
        if (!returnValue)
            Console.WriteLine
                ("UserQuota 100 failed");

        Console.WriteLine
            ("Requesting UserQuota of -2147483648.");
        returnValue = UserQuota(isfp0, -2147483648, true);
        if (!returnValue)
            Console.WriteLine
                ("UserQuota -214748364 failed");
    }

    // IsSubsetOfDemo demonstrates the IsSubsetOf method.
    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    private void IsSubsetOfDemo()
    {
        Console.WriteLine("\nIsSubsetOf demo.");

        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp1 = new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp2 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp3 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);

        if (isfp0.IsSubsetOf(isfp1))
        {
            Console.WriteLine
                ("PermissionState.None is a subset of PermissionState.None.");
        }
        else
        {
            Console.WriteLine
                ("IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.None");
        }

        if (isfp0.IsSubsetOf(isfp2))
        {
            Console.WriteLine
                ("PermissionState.None is a subset of PermissionState.Unrestricted.");
        }
        else
        {
            Console.WriteLine
                ("IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.Unrestricted");
        }

        if (!isfp2.IsSubsetOf(isfp0))
        {
            Console.WriteLine
                ("PermissionState.Unrestricted is not a subset of PermissionState.None.");
        }
        else
        {
            Console.WriteLine
                ("IsSubsetOfDemo failed: PermissionState.Unrestricted should not be a subset " +
                "\n\tof PermissionState.None");
        }

        if (isfp2.IsSubsetOf(isfp3))
        {
            Console.WriteLine
                ("PermissionState.Unrestricted is a subset of PermissionState.Unrestricted.");
        }
        else
        {
            Console.WriteLine
                ("IsSubsetOfDemo failed: Permissionstate.Unrestricted should be a subset of " +
                "\n\tPermissionState.Unrestricted");
        }

    }

    // **********************************************************************
    // The following methods are simple helper functions.
    // **********************************************************************

    // **********************************************************************
    // UserQuota
    // Sets UserQuota to the value that was passed in.
    //

    private bool UserQuota(IsolatedStorageFilePermission isfp, int user_quota, bool exception_expected)
    {
        bool ExceptionCaught = false;
        isfp.UserQuota = user_quota;

        try
        {
            isfp.Demand();
        }
        catch (Exception e)
        {
            ExceptionCaught = true;
            if (!exception_expected)
            {
                Console.WriteLine
                    ("An unexpected exception was thrown when setting UserQuota value [{0}]", user_quota.ToString());
                Console.WriteLine("Exception: \r\n" + e.ToString());
            }
        }

        if ((!ExceptionCaught) && exception_expected)
        {
            Console.WriteLine
                ("An expected exception not thrown when setting UserQuota value [{0}]", user_quota.ToString());
        }

        return true;
    }

    // **********************************************************************
    // UsageAllowed
    // This method sets UsageAllowed to the value that was passed in.
    //

    private bool UsageAllowed(IsolatedStorageFilePermission isfp, IsolatedStorageContainment isc, bool exception_expected)
    {
        bool ExceptionCaught = false;
        isfp.UsageAllowed = isc;
        try
        {
            isfp.Demand();
        }
        catch (Exception e)
        {
            ExceptionCaught = true;
            if (!exception_expected)

            {
                Console.WriteLine
                    ("An unexpected exception was thrown when setting IsolatedStorageContainment value [{0}]", isc.ToString());
                Console.WriteLine("Exception: \r\n" + e.ToString());
            }
        }

        if ((!ExceptionCaught) && exception_expected)
        {
            Console.WriteLine
                ("An expected exception was not thrown when setting IsolatedStorageContainment value [{0}]", isc.ToString());
        }

        return true;
    }


    // **********************************************************************
    // DoDemand()
    // This method demands an IsolatedStorageFilePermission.

    private int DoDemand()
    {
        int ReturnValue = 0;
        IsolatedStorageFilePermission isfp0 = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        try
        {
            isfp0.Demand();
        }
        catch (SecurityException)
        {
            ReturnValue = 1;
        }
        catch (Exception e)
        {
            Console.WriteLine
                ("An unexpected exception was thrown in DoDemand\r\n" + e.ToString());
            ReturnValue = -1;
        }

        return ReturnValue;
    }

    // **********************************************************************
    // DemandUI()
    // This method demands a UIPermission
    // Returns:
    // 0 if the demand was successful.
    // 1 if a SecurityException was thrown.
    // -1 if an unexpected exception was thrown.
    //
    private int DemandUI()
    {
        int ReturnValue = 0;
        UIPermission MyPermission = new UIPermission(PermissionState.Unrestricted);
        try
        {
            MyPermission.Demand();
        }
        catch (SecurityException)
        {
            ReturnValue = 1;
        }
        catch (Exception e)
        {
            Console.WriteLine("An unexpected exception was thrown in DemandUI\r\n" + e.ToString());
        }

        return ReturnValue;
    }


    public static int Main(String[] args)
    {
        Demo MyDemo = new Demo();

        MyDemo.Run();
        return 0 ;
    }
}
using namespace System;
using namespace System::Security;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;
using namespace System::Security::Permissions;
using namespace System::Diagnostics;
public ref class Demo
{
public:

   // Main method.
   void Run()
   {
      try
      {
         Copy_EqualsDemo();
         To_FromXmlDemo();
         GetTypeDemo();
         IntersectDemo();
         UnionDemo();
         ToStringDemo();
         Demand_DenyDemo();
         PermitOnlyDemo();
         UserQuota_UsageDemo();
         IsSubsetOfDemo();
         IsUnrestrictedDemo();
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "\r\n\nThe demo failed due to an unexpected exception\r\n{0}", e );
      }

   }


private:

   // IsUnrestrictedDemo demonstrates the IsolatedStorageFilePermission.IsUnrestricted method.
   // IsUnrestrictedDemo displays a comment indicating whether the current permission is unrestricted.
   void IsUnrestrictedDemo()
   {
      Console::WriteLine( "IsUnrestricted demo." );
      try
      {
         IsolatedStorageFilePermission^ isfp = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
         isfp->Demand(); // The permission should be unrestricted.
         Console::WriteLine( "Created and demanded the IsolatedStorageFilePermission(PermissionState.Unrestricted)permission." );
         if ( true != isfp->IsUnrestricted() )
         {
            Console::WriteLine( "IsUnrestricted demo failed. The IsUnrestricted property value should be 'true'" );
         }
         isfp = gcnew IsolatedStorageFilePermission( PermissionState::None );
         isfp->Demand(); // The permission should not be unrestricted.
         Console::WriteLine( "Created and demanded the IsolatedStorageFilePermission(PermissionState.None)permission." );
         if ( false != isfp->IsUnrestricted() )
         {
            Console::WriteLine( "IsUnrestricted demo failed. The IsUnrestricted property value should be 'false'" );
         }
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Unexpected exception: {0}", e->Message );
      }

   }


   // Copy_EqualsDemo demonstrates the Copy and Equals methods.
   // Copy creates and returns an identical copy of the current permission.
   // Equals compares two permissions and determines whether they are equal.
   void Copy_EqualsDemo()
   {
      Console::WriteLine( "\nCopy_Equals demo." );
      Console::WriteLine( "Creating the first IsolatedStorageFilePermission(PermissionState.Unrestricted) permission." );
      IsolatedStorageFilePermission^ first = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      Console::WriteLine( "Hashcode for the first permission : {0}", first->GetHashCode() );
      Console::WriteLine( "Copying the first permission." );
      IsolatedStorageFilePermission^ copyOfFirst = dynamic_cast<IsolatedStorageFilePermission^>(first->Copy());
      Console::WriteLine( "Hashcode for copy of the first permission: {0}", copyOfFirst->GetHashCode() );
      if ( false == copyOfFirst->Equals( first ) )
      {
         Console::WriteLine( "The copy and the first permission should not be equal because they have "
         "\n\tdifferent object references." );
      }
      else
      {
         Console::WriteLine( "Error: the copy and the first permission should not be equal because they have "
         "\n\tdifferent object references." );
      }

      Console::WriteLine( "Does a comparison of copy of first permission to itself test equal??: {0}", (copyOfFirst->Equals( copyOfFirst ) ? (String^)"true" : "false") );
      if ( false == copyOfFirst->Equals( copyOfFirst ) )
      {
         Console::WriteLine( "Copy_Equals demo failed because Equals returns false on two object.Equals(itself)." );
      }

      Console::WriteLine( "Does a comparison of first permission to itself test equal?: {0}", (first->Equals( first ) ? (String^)"true" : "false") );
      if ( false == first->Equals( first ) )
      {
         Console::WriteLine( "Copy_Equals demo failed, because Equals returns false on two object.Equals(itself)." );
      }

      Console::WriteLine( "Creating a second permission with PermissionState.None." );
      IsolatedStorageFilePermission^ second = gcnew IsolatedStorageFilePermission( PermissionState::None );
      Console::WriteLine( "Does the first permission equal the second permission?: {0}", (first->Equals( second ) ? (String^)"true" : "false") );
      if ( true == first->Equals( second ) )
      {
         Console::WriteLine( "Copy_Equals demo failed because Equals returns false on two object.Equals(itself)." );
      }
   }


   // To_FromXmlDemo demonstrates ToXml and FromXml.
   // ToXml creates an XML encoding of the permission and its current state.
   // FromXml reconstructs a permission with a specified state from an XML encoding.
   void To_FromXmlDemo()
   {
      Console::WriteLine( "\nTo_FromXML demo." );
      IsolatedStorageFilePermission^ firstPermission = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      Console::WriteLine( "First permission: {0}", firstPermission );
      SecurityElement^ se = firstPermission->ToXml();
      IsolatedStorageFilePermission^ secondPermission = gcnew IsolatedStorageFilePermission( PermissionState::None );
      Console::WriteLine( "Second permission: {0}", secondPermission );
      secondPermission->FromXml( se );
      IsolatedStorageFilePermission^ thirdPermission = dynamic_cast<IsolatedStorageFilePermission^>(secondPermission->Intersect( firstPermission ));
      Console::WriteLine( "Intersection of first permission and second permission: {0}", thirdPermission );
      try
      {
         if ( false == thirdPermission->IsUnrestricted() )
         {
            Console::WriteLine( "To_FromXml demo failed: The ToXml, FromXml roundtrip did not succeed." );
         }
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "To_FromXml demo failed: An exception was thrown when checking the intersection" );
         Console::WriteLine( "of the permissions after the XML roundtrip." );
         Console::WriteLine( e );
         Console::WriteLine( e );
      }

   }


   // GetType demonstrates the GetType method.
   // This method returns the type of the current instance.
   void GetTypeDemo()
   {
      Console::WriteLine( "\nGetType demo." );
      IsolatedStorageFilePermission^ isfp = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      if ( 0 != isfp->GetType()->ToString()->CompareTo( "System.Security.Permissions.IsolatedStorageFilePermission" ) )
      {
         Console::WriteLine( "GetType returned the wrong value: {0}", isfp->GetType() );
      }
      else
      {
         Console::WriteLine( "GetType returned: {0}", isfp->GetType() );
      }
   }


   // IntersectDemo demonstrates the Intersect method.
   // CIntersect creates and returns a permission that is the intersection of the current permission
   // and the specified permission.
   // Note  The intersection of two PermissionState.None permissions *IS* null. 
   void IntersectDemo()
   {
      Console::WriteLine( "\nIntersect demo." );
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      IsolatedStorageFilePermission^ isfp2 = gcnew IsolatedStorageFilePermission( PermissionState::None );
      IsolatedStorageFilePermission^ isfp3 = gcnew IsolatedStorageFilePermission( PermissionState::None );
      IsolatedStorageFilePermission^ t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Intersect( isfp1 ));
      if ( true == t->IsUnrestricted() )
      {
         Console::WriteLine( "The intersection of two PermissionState.Unrestricted permissions is unrestricted." );
      }
      else
      {
         Console::WriteLine( "Intersect demo failed because the intersection of two unrestricted permissions "
         "\n\tshould be unrestricted." );
      }

      t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Intersect( isfp2 ));
      if ( false == t->IsUnrestricted() )
      {
         Console::WriteLine( "The intersection of a PermissionState.Unrestricted and "
         "\n\tPermissionState.None permission is not unrestricted." );
      }
      else
      {
         Console::WriteLine( "Intersect demo failed because the intersection of a unrestricted and "
         "\n\tnone permission should be none." );
      }

      
      // The permission set that comes back from this intersection is null.
      t = dynamic_cast<IsolatedStorageFilePermission^>(isfp2->Intersect( isfp3 ));
      if ( nullptr == t )
      {
         Console::WriteLine( "The intersection of two PermissionState.None permissions is null." );
      }
      else
      {
         Console::WriteLine( "Intersect failed because the intersection of two PermissionState.None permissions "
         "\n\tshould be null." );
      }
   }


   // UnionDemo demonstrates the Union method.
   // Union creates a permission that is the union of the current permission and the specified permission.
   // Note: The union of two PermissionState.None permissions is *NOT* null.
   void UnionDemo()
   {
      Console::WriteLine( "\nUnion demo." );
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      IsolatedStorageFilePermission^ isfp2 = gcnew IsolatedStorageFilePermission( PermissionState::None );
      IsolatedStorageFilePermission^ isfp3 = gcnew IsolatedStorageFilePermission( PermissionState::None );
      IsolatedStorageFilePermission^ t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Union( isfp1 ));
      if ( true == t->IsUnrestricted() )
      {
         Console::WriteLine( "The union of two PermissionState.Unrestricted permissions is unrestricted." );
      }
      else
      {
         Console::WriteLine( "Union demo failed because the union of two PermissionState.Unrestricted permissions "
         "\n\tshould be unrestricted" );
      }

      t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Union( isfp2 ));
      if ( true == t->IsUnrestricted() )
      {
         Console::WriteLine( "The union of a PermissionState.Unrestricted and a PermissionState.None "
         "\n\tpermission is unrestricted." );
      }
      else
      {
         Console::WriteLine( "Union demo failed because the union of a PermissionState.Unrestricted and a "
         "\n\tPermissionState.None permission should be unrestricted." );
      }

      t = dynamic_cast<IsolatedStorageFilePermission^>(isfp2->Union( isfp3 ));
      if ( nullptr != t )
      {
         Console::WriteLine( "The union of two PermissionState.None permissions is none." );
      }
      else
      {
         Console::WriteLine( "Union failed because the union of two PermissionState.None permissions should not be null." );
         Console::WriteLine( t );
      }
   }


   // ToStringDemo demonstrates the ToString method.
   // ToString creates and returns a string representation of the current permission object.
   void ToStringDemo()
   {
      Console::WriteLine( "\nToString demo." );
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      String^ PermString = isfp0->ToString();
      array<String^>^PermStringParts = {"class=","mscorlib","Version=","Culture=","PublicKeyToken=","version=","Unrestricted="};
      System::Collections::IEnumerator^ myEnum = PermStringParts->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         String^ part = safe_cast<String^>(myEnum->Current);
         if ( PermString->IndexOf( part ) >= 0 )
         {
            Console::WriteLine( "{0} found in the string.", part );
         }
         else
         {
            Console::WriteLine( "ToString demo failed because [{0}] was not found.", part );
         }
      }
   }


   // Demand_Deny demonstrates Demand and Deny.
   // Demand forces a SecurityException at run time if all callers higher in the call stack 
   // have not been granted the permission specified by the current instance.
   // Deny prevents callers higher in the call stack from using the code that calls this method to 
   // access the resource specified by the current instance.
   void Demand_DenyDemo()
   {
      Console::WriteLine( "\nDemand_Deny demo." );
      Console::WriteLine( "Denying a permission with PermissionState.Unrestricted." );
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      isfp0->Deny();
      try
      {
         Console::WriteLine( "Denying permission a second time." );
         isfp0->Deny();
      }
      catch ( SecurityException^ ) 
      {
         Console::WriteLine( "SecurityException was thrown as expected." );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( String::Format( "Demand_Deny demo failed because an unexpected exception was thrown"
         "\n\twhen making a second call to Deny()\r\n{0}", e ) );
      }

      Console::WriteLine( "Reverting the deny." );
      IsolatedStorageFilePermission::RevertDeny();
      if ( 0 != DoDemand() )
      {
         Console::WriteLine( "Demand_Deny demo failed because an unexpected exception was thrown during a demand." );
      }

      isfp0->Deny();
      if ( 1 != DoDemand() )
      {
         Console::WriteLine( "Demand_Deny demo failed because the expected SecurityException was not thrown during a demand." );
      }

      IsolatedStorageFilePermission::RevertDeny();
      if ( 0 != DoDemand() )
      {
         Console::WriteLine( "Demand_Deny demo failed because an unexpected exception was thrown during a demand." );
      }
   }


   // PermitOnly demonstrates the PermitOnly method.
   // PermitOnly prevents callers higher in the call stack from using the code that calls this method to 
   // access all resources except for the resource specified by the current instance.
   void PermitOnlyDemo()
   {
      Console::WriteLine( "\nPermitOnly demo." );
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      isfp0->Demand();
      isfp0->PermitOnly();
      try
      {
         isfp0->PermitOnly();
      }
      catch ( SecurityException^ ) 
      {
         Console::WriteLine( "The second call to PermitOnly caused an exception as expected." );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( String::Format( "PermitOnly demo failed because an unexpected exception was thrown"
         "\n\twhen making a second call to PermitOnly()\r\n{0}", e ) );
      }

      IsolatedStorageFilePermission::RevertPermitOnly();
      if ( 0 != DemandUI() )
      {
         Console::WriteLine( "PermitOnly demo failed because an exception was thrown during a demand for UI permissions." );
         Console::WriteLine( "We weren't expecting this." );
      }

      isfp0->PermitOnly();
      if ( 1 != DemandUI() )
      {
         Console::WriteLine( "PermitOnly demo failed because a SecurityException was not thrown, "
         "\n\tduring a demand for UI permissions.  A SecurityException was expected." );
      }

      IsolatedStorageFilePermission::RevertPermitOnly();
      if ( 0 != DoDemand() )
      {
         Console::WriteLine( "PermitOnly failed because an exception was thrown during a demand for UI permissions." );
         Console::WriteLine( "We weren't expecting this." );
      }
   }


   // UserQuota_UsageDemo demonstrates UserQuota and UsageAllowed properties.
   // UserQuota gets or sets the quota on the overall size of each user's total store.  
   // This property is inherited from IsolatedStoragePermission.UserQuota.
   // UsageAllowed gets or sets the type of isolated storage containment allowed.  
   // This property is inherited from IsolatedStoragePermission.UsageAllowed.
   // Note  You can set UsageAllowed to None, but you cannot set UserQuota to 0.
   // Also note that trying to set UsageAllowed or UserQuota to values that are currently set
   // will throw an exception.  

   [method:IsolatedStorageFilePermissionAttribute(SecurityAction::Deny,UsageAllowed=IsolatedStorageContainment::DomainIsolationByRoamingUser,UserQuota=100)]
   void UserQuota_UsageDemo()
   {
      Console::WriteLine( "\nUserQuota_Usage demo." );
      bool returnValue = false;
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::None );
      Console::WriteLine( "Checking UsageAllowed for IsolatedStorageContainment.None" );
      returnValue = UsageAllowed( isfp0, IsolatedStorageContainment::None, false );
      if (  !returnValue )
            Console::WriteLine( "IsolatedStorageContainment.None demo failed" );

      Console::WriteLine( "Checking UsageAllowed for IsolatedStorageContainment.DomainIsolationByRoamingUser." );
      returnValue = UsageAllowed( isfp0, IsolatedStorageContainment::DomainIsolationByRoamingUser, true );
      if (  !returnValue )
            Console::WriteLine( "IsolatedStorageContainment.DomainIsolationByRoamingUser failed" );

      Console::WriteLine( "Checking UsageAllowed for IsolatedStorageContainment.AssemblyIsolationByUser." );
      returnValue = UsageAllowed( isfp0, IsolatedStorageContainment::AssemblyIsolationByUser, true );
      if (  !returnValue )
            Console::WriteLine( "IsolatedStorageContainment.AssemblyIsolationByUse failed" );

      Console::WriteLine( "Requesting UserQuota of 0." );
      returnValue = UserQuota( isfp0, 0, true );
      if (  !returnValue )
            Console::WriteLine( "UserQuota 0 failed" );

      Console::WriteLine( "Requesting UserQuota of 100." );
      returnValue = UserQuota( isfp0, 100, true );
      if (  !returnValue )
            Console::WriteLine( "UserQuota 100 failed" );

      Console::WriteLine( "Requesting UserQuota of -2147483648." );
      returnValue = UserQuota( isfp0, Int32::MinValue, true );
      if (  !returnValue )
            Console::WriteLine( "UserQuota -214748364 failed" );
   }


   // IsSubsetOfDemo demonstrates the IsSubsetOf method.
   // IsSubsetOf determines whether the current permission is a subset of the specified permission.
   void IsSubsetOfDemo()
   {
      Console::WriteLine( "\nIsSubsetOf demo." );
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::None );
      IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::None );
      IsolatedStorageFilePermission^ isfp2 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      IsolatedStorageFilePermission^ isfp3 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      if ( isfp0->IsSubsetOf( isfp1 ) )
      {
         Console::WriteLine( "PermissionState.None is a subset of PermissionState.None." );
      }
      else
      {
         Console::WriteLine( "IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.None" );
      }

      if ( isfp0->IsSubsetOf( isfp2 ) )
      {
         Console::WriteLine( "PermissionState.None is a subset of PermissionState.Unrestricted." );
      }
      else
      {
         Console::WriteLine( "IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.Unrestricted" );
      }

      if (  !isfp2->IsSubsetOf( isfp0 ) )
      {
         Console::WriteLine( "PermissionState.Unrestricted is not a subset of PermissionState.None." );
      }
      else
      {
         Console::WriteLine( "IsSubsetOfDemo failed: PermissionState.Unrestricted should not be a subset "
         "\n\tof PermissionState.None" );
      }

      if ( isfp2->IsSubsetOf( isfp3 ) )
      {
         Console::WriteLine( "PermissionState.Unrestricted is a subset of PermissionState.Unrestricted." );
      }
      else
      {
         Console::WriteLine( "IsSubsetOfDemo failed: Permissionstate.Unrestricted should be a subset of "
         "\n\tPermissionState.Unrestricted" );
      }
   }


   // **********************************************************************
   // The following methods are simple helper functions.
   // **********************************************************************
   // **********************************************************************
   // UserQuota
   // Sets UserQuota to the value that was passed in.
   //
   bool UserQuota( IsolatedStorageFilePermission^ isfp, int user_quota, bool exception_expected )
   {
      bool ExceptionCaught = false;
      isfp->UserQuota = user_quota;
      try
      {
         isfp->Demand();
      }
      catch ( Exception^ e ) 
      {
         ExceptionCaught = true;
         if (  !exception_expected )
         {
            Console::WriteLine( "An unexpected exception was thrown when setting UserQuota value [{0}]", user_quota );
            Console::WriteLine( "Exception: \r\n{0}", e );
         }
      }

      if ( ( !ExceptionCaught) && exception_expected )
      {
         Console::WriteLine( "An expected exception not thrown when setting UserQuota value [{0}]", user_quota );
      }

      return true;
   }


   // **********************************************************************
   // UsageAllowed
   // This method sets UsageAllowed to the value that was passed in.
   //
   bool UsageAllowed( IsolatedStorageFilePermission^ isfp, IsolatedStorageContainment isc, bool exception_expected )
   {
      bool ExceptionCaught = false;
      isfp->UsageAllowed = isc;
      try
      {
         isfp->Demand();
      }
      catch ( Exception^ e ) 
      {
         ExceptionCaught = true;
         if (  !exception_expected )
         {
            Console::WriteLine( "An unexpected exception was thrown when setting IsolatedStorageContainment value [{0}]", isc );
            Console::WriteLine( "Exception: \r\n{0}", e );
         }
      }

      if ( ( !ExceptionCaught) && exception_expected )
      {
         Console::WriteLine( "An expected exception was not thrown when setting IsolatedStorageContainment value [{0}]", isc );
      }

      return true;
   }


   // **********************************************************************
   // DoDemand()
   // This method demands an IsolatedStorageFilePermission.
   int DoDemand()
   {
      int ReturnValue = 0;
      IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
      try
      {
         isfp0->Demand();
      }
      catch ( SecurityException^ ) 
      {
         ReturnValue = 1;
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "An unexpected exception was thrown in DoDemand\r\n{0}", e );
         ReturnValue = -1;
      }

      return ReturnValue;
   }


   // **********************************************************************
   // DemandUI()
   // This method demands a UIPermission
   // Returns:
   //    0 if the demand was successful.
   //    1 if a SecurityException was thrown.
   //    -1 if an unexpected exception was thrown.
   //
   int DemandUI()
   {
      int ReturnValue = 0;
      UIPermission^ MyPermission = gcnew UIPermission( PermissionState::Unrestricted );
      try
      {
         MyPermission->Demand();
      }
      catch ( SecurityException^ ) 
      {
         ReturnValue = 1;
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "An unexpected exception was thrown in DemandUI\r\n{0}", e );
      }

      return ReturnValue;
   }

};

int main()
{
   Demo^ MyDemo = gcnew Demo;
   MyDemo->Run();
   return 0;
}
import System .* ;
import System.Security .* ;
import System.IO .* ;
import System.IO.IsolatedStorage .* ;
import System.Security.Permissions .* ;
import System.Diagnostics .* ;

public class Demo
{
    // Main method.
    public void Run()
    {
        try {
            Copy_EqualsDemo();
            To_FromXmlDemo();
            GetTypeDemo();
            IntersectDemo();
            UnionDemo();
            ToStringDemo();
            Demand_DenyDemo();
            PermitOnlyDemo();
            UserQuota_UsageDemo();
            IsSubsetOfDemo();
            IsUnrestrictedDemo();
        }
        catch (System.Exception e) {
            Console.WriteLine(("\r\n\nThe demo failed due to an " 
                + "unexpected exception\r\n" + e.ToString()));
        }
    } //Run

    // IsUnrestrictedDemo demonstrates the 
    // IsolatedStorageFilePermission.IsUnrestricted method.
    // IsUnrestrictedDemo displays a comment indicating whether 
    // the current permission is unrestricted.
    private void IsUnrestrictedDemo()
    {
        Console.WriteLine("IsUnrestricted demo.");
        try {
            IsolatedStorageFilePermission isfp = 
                new IsolatedStorageFilePermission(PermissionState.Unrestricted);
            isfp.Demand(); // The permission should be unrestricted.
            Console.WriteLine("Created and demanded the " 
                + "IsolatedStorageFilePermission(PermissionState." 
                + "Unrestricted)permission.");
            if (true != isfp.IsUnrestricted()) {
                Console.WriteLine("IsUnrestricted demo failed. " 
                    + "The IsUnrestricted property value should be 'true'");
            }
            isfp = new IsolatedStorageFilePermission(PermissionState.None);
            isfp.Demand(); // The permission should not be unrestricted.
            Console.WriteLine("Created and demanded the " 
                + "IsolatedStorageFilePermission(PermissionState.None)"
                + "permission.");
            if (false != isfp.IsUnrestricted()) {
                Console.WriteLine("IsUnrestricted demo failed. The " 
                    + "IsUnrestricted property value should be 'false'");
            }
        }
        catch (System.Exception e) {
            Console.WriteLine(("Unexpected exception: " + e.get_Message()));
        }
    } //IsUnrestrictedDemo   

    // Copy_EqualsDemo demonstrates the Copy and Equals methods.
    // Copy creates and returns an identical copy of the current permission.
    // Equals compares two permissions and determines whether they are equal.
    private void Copy_EqualsDemo()
    {
        Console.WriteLine("\nCopy_Equals demo.");
        Console.WriteLine("Creating the first " 
            + "IsolatedStorageFilePermission(PermissionState." 
            + "Unrestricted) permission.");
        IsolatedStorageFilePermission first = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        Console.WriteLine(("Hashcode for the first permission : " 
            + System.Convert.ToString(first.GetHashCode())));
        Console.WriteLine("Copying the first permission.");
        IsolatedStorageFilePermission copyOfFirst = 
            ((IsolatedStorageFilePermission)(first.Copy()));
        Console.WriteLine(("Hashcode for copy of the first permission: " 
            + System.Convert.ToString(copyOfFirst.GetHashCode())));
        if (false == copyOfFirst.Equals(first)) {
            // This should be false, because the object references are 
            // different.
            Console.WriteLine(("The copy and the first permission " 
                + "should not be equal because they have " 
                + "\n\tdifferent object references."));
        }
        else {
            Console.WriteLine(("Error: the copy and the first " 
                + "permission should not be equal because they have " 
                + "\n\tdifferent object references."));
        }
        Console.WriteLine("Does a comparison of copy of first " 
            + "permission to itself test equal??: " 
            + ((copyOfFirst.Equals(copyOfFirst)) ? "true" : "false"));
        if (false == copyOfFirst.Equals(copyOfFirst)) {
            Console.WriteLine("Copy_Equals demo failed because Equals " 
                + "returns false on two object.Equals(itself).");
        }
        Console.WriteLine(("Does a comparison of first permission to " 
            + "itself test equal?: " 
            + ((first.Equals(first)) ? "true" : "false")));
        if (false == first.Equals(first)) {
            Console.WriteLine("Copy_Equals demo failed, because Equals " 
                + "returns false on two object.Equals(itself).");
        }
        Console.WriteLine("Creating a second permission with " 
            + "PermissionState.None.");
        IsolatedStorageFilePermission second = 
            new IsolatedStorageFilePermission(PermissionState.None);
        Console.WriteLine(("Does the first permission equal the " 
            + "second permission?: " 
            + ((first.Equals(second)) ? "true" : "false")));
        if (true == first.Equals(second)) {
            Console.WriteLine("Copy_Equals demo failed because Equals " 
                + "returns false on two object.Equals(itself).");
        }
    } //Copy_EqualsDemo   

    // To_FromXmlDemo demonstrates ToXml and FromXml.
    // ToXml creates an XML encoding of the permission and its current state.
    // FromXml reconstructs a permission with a specified state from 
    // an XML encoding.
    private void To_FromXmlDemo()
    {
        Console.WriteLine("\nTo_FromXML demo.");
        IsolatedStorageFilePermission firstPermission = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        Console.WriteLine(("First permission: " + firstPermission.ToString()));
        SecurityElement se = firstPermission.ToXml();
        IsolatedStorageFilePermission secondPermission = 
            new IsolatedStorageFilePermission(PermissionState.None);
        Console.WriteLine(("Second permission: " 
            + secondPermission.ToString()));
        secondPermission.FromXml(se);
        IsolatedStorageFilePermission thirdPermission = 
            ((IsolatedStorageFilePermission)
            (secondPermission.Intersect(firstPermission)));
        Console.WriteLine(("Intersection of first permission and " 
            + "second permission: " + thirdPermission.ToString()));

        try {
            if (false == thirdPermission.IsUnrestricted()) {
                Console.WriteLine("To_FromXml demo failed: The ToXml, " 
                    + "FromXml roundtrip did not succeed.");
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("To_FromXml demo failed: An exception " 
                + "was thrown when checking the intersection");
            Console.WriteLine("of the permissions after the XML roundtrip.");
            Console.WriteLine(e.ToString());
            Console.WriteLine(e.ToString());
        }
    } //To_FromXmlDemo   

    // GetType demonstrates the GetType method.
    // This method returns the type of the current instance.
    private void GetTypeDemo()
    {
        Console.WriteLine("\nGetType demo.");
        IsolatedStorageFilePermission isfp = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp1 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        if (0 != isfp.GetType().toString().CompareTo(
            "System.Security.Permissions.IsolatedStorageFilePermission")) {
            Console.WriteLine(("GetType returned the wrong value: " 
            + isfp.GetType().toString()));
        }
        else {
            Console.WriteLine(("GetType returned: " 
                + isfp.GetType().toString()));
        }
    } //GetTypeDemo   

    // IntersectDemo demonstrates the Intersect method.
    // CIntersect creates and returns a permission that is the intersection 
    // of the current permission and the specified permission.
    // Note The intersection of two PermissionState.None permissions *IS* null.
    private void IntersectDemo()
    {
        Console.WriteLine("\nIntersect demo.");
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp1 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp2 = 
            new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp3 = 
            new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission t = 
            ((IsolatedStorageFilePermission)(isfp0.Intersect(isfp1)));
        if (true == t.IsUnrestricted()) {
            Console.WriteLine("The intersection of two " 
                + "PermissionState.Unrestricted permissions is unrestricted.");
        }
        else {
            Console.WriteLine(("Intersect demo failed because the " 
                + "intersection of two unrestricted permissions " 
                + "\n\tshould be unrestricted."));
        }
        t = ((IsolatedStorageFilePermission)(isfp0.Intersect(isfp2)));
        if (false == t.IsUnrestricted()) {
            Console.WriteLine(("The intersection of a " 
                + "PermissionState.Unrestricted and " 
                + "\n\tPermissionState.None permission is not unrestricted."));
        }
        else {
            Console.WriteLine(("Intersect demo failed because the " 
                + "intersection of a unrestricted and " 
                + "\n\tnone permission should be none."));
        }

        // The permission set that comes back from this intersection is null.
        t = ((IsolatedStorageFilePermission)(isfp2.Intersect(isfp3)));
        if (null == t) {
            Console.WriteLine("The intersection of two " 
                + "PermissionState.None permissions is null.");
        }
        else {
            Console.WriteLine(("Intersect failed because the " 
                + "intersection of two PermissionState.None permissions " 
                + "\n\tshould be null."));
        }
    } //IntersectDemo   

    // UnionDemo demonstrates the Union method.
    // Union creates a permission that is the union of the current 
    // permission and the specified permission.
    // Note: The union of two PermissionState.None permissions is *NOT* null.
    private void UnionDemo()
    {
        Console.WriteLine("\nUnion demo.");
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp1 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp2 = 
            new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp3 = 
            new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission t = 
            ((IsolatedStorageFilePermission)(isfp0.Union(isfp1)));

        if (true == t.IsUnrestricted()) {
            Console.WriteLine("The union of two " 
                + "PermissionState.Unrestricted permissions is unrestricted.");
        }
        else {
            Console.WriteLine(("Union demo failed because the union " 
                + "of two PermissionState.Unrestricted permissions " 
                + "\n\tshould be unrestricted"));
        }
        t = ((IsolatedStorageFilePermission)(isfp0.Union(isfp2)));
        if (true == t.IsUnrestricted()) {
            Console.WriteLine(("The union of a PermissionState.Unrestricted " 
                + "and a PermissionState.None " 
                + "\n\tpermission is unrestricted."));
        }
        else {
            Console.WriteLine(("Union demo failed because the union of a " 
                + "PermissionState.Unrestricted and a " 
                + "\n\tPermissionState.None permission should be "
                + "unrestricted."));
        }
        t = ((IsolatedStorageFilePermission)(isfp2.Union(isfp3)));
        if (null != t) {
            Console.WriteLine("The union of two PermissionState.None " 
                + "permissions is none.");
        }
        else {
            Console.WriteLine("Union failed because the union of two " 
                + "PermissionState.None permissions should not be null.");
            Console.WriteLine(t.ToString());
        }
    } //UnionDemo   

    // ToStringDemo demonstrates the ToString method.
    // ToString creates and returns a string representation of the current 
    // permission object.
    private void ToStringDemo()
    {
        Console.WriteLine("\nToString demo.");
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        String permString = isfp0.ToString();
        String permStringParts[] = {"class=", "mscorlib", "Version=", 
            "Culture=", "PublicKeyToken=", "version=", "Unrestricted="};

        for (int iCtr = 0; iCtr < permStringParts.length; iCtr++) {
            String part = permStringParts[iCtr];
            if (permString.IndexOf(part) >= 0) {
                Console.WriteLine((part + " found in the string."));
            }
            else {
                Console.WriteLine("ToString demo failed because [{0}] " 
                    + "was not found.", part);
            }
        }
    } //ToStringDemo    

    // Demand_Deny demonstrates Demand and Deny.
    // Demand forces a SecurityException at run time if all callers higher 
    // in the call stack have not been granted the permission specified 
    // by the current instance.
    // Deny prevents callers higher in the call stack from using the code 
    // that calls this method to
    // access the resource specified by the current instance.
    private void Demand_DenyDemo()
    {
        Console.WriteLine("\nDemand_Deny demo.");
        Console.WriteLine("Denying a permission with " 
            + "PermissionState.Unrestricted.");
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        isfp0.Deny();

        try {
            Console.WriteLine("Denying permission a second time.");
            isfp0.Deny();
        }
        catch (SecurityException exp) {
            Console.WriteLine("SecurityException was thrown as expected.");
        }
        catch (System.Exception e) {
            Console.WriteLine(("Demand_Deny demo failed because an " 
                + "unexpected exception was thrown" 
                + "\n\twhen making a second call to Deny()\r\n" 
                + e.ToString()));
        }
        Console.WriteLine("Reverting the deny.");
        IsolatedStorageFilePermission.RevertDeny();
        if (0 != DoDemand()) {
            Console.WriteLine("Demand_Deny demo failed because an " 
                + "unexpected exception was thrown during a demand.");
        }
        isfp0.Deny();
        if (1 != DoDemand()) {
            Console.WriteLine("Demand_Deny demo failed because the " 
                + "expected SecurityException was not thrown during a demand.");
        }
        IsolatedStorageFilePermission.RevertDeny();
        if (0 != DoDemand()) {
            Console.WriteLine("Demand_Deny demo failed because an " 
                + "unexpected exception was thrown during a demand.");
        }
    } //Demand_DenyDemo   

    // PermitOnly demonstrates the PermitOnly method.
    // PermitOnly prevents callers higher in the call stack from using the 
    // code that calls this method to access all resources except for the 
    // resource specified by the current instance.
    private void PermitOnlyDemo()
    {
        Console.WriteLine("\nPermitOnly demo.");
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        isfp0.Demand();
        isfp0.PermitOnly();

        try {
            isfp0.PermitOnly();
        }
        catch (SecurityException exp) {
            Console.WriteLine("The second call to PermitOnly caused an " 
                + "exception as expected.");
        }
        catch (System.Exception e) {
            Console.WriteLine(("PermitOnly demo failed because an " 
                + "unexpected exception was thrown" 
                + "\n\twhen making a second call to PermitOnly()\r\n" 
                + e.ToString()));
        }
        IsolatedStorageFilePermission.RevertPermitOnly();
        if (0 != DemandUI()) {
            Console.WriteLine("PermitOnly demo failed because an " 
                + "exception was thrown during a demand for UI permissions.");
            Console.WriteLine("We weren't expecting this.");
        }
        isfp0.PermitOnly();
        if (1 != DemandUI()) {
            Console.WriteLine(("PermitOnly demo failed because a " 
                + "SecurityException was not thrown, " 
                + "\n\tduring a demand for UI permissions. " 
                + "A SecurityException was expected."));
        }
        IsolatedStorageFilePermission.RevertPermitOnly();
        if (0 != DoDemand()) {
            Console.WriteLine("PermitOnly failed because an exception " 
                + "was thrown during a demand for UI permissions.");
            Console.WriteLine("We weren't expecting this.");
        }
    } //PermitOnlyDemo   

    // UserQuota_UsageDemo demonstrates UserQuota and UsageAllowed properties.
    // UserQuota gets or sets the quota on the overall size of each user's 
    // total store. This property is inherited from 
    // IsolatedStoragePermission.UserQuota. UsageAllowed gets or sets the 
    // type of isolated storage containment allowed. This property is inherited 
    // from IsolatedStoragePermission.UsageAllowed. Note You can set 
    // UsageAllowed to None, but you cannot set UserQuota to 0. Also note that 
    // trying to set UsageAllowed or UserQuota to values that are currently set 
    // will throw an exception.
    /** @attribute.method IsolatedStorageFilePermissionAttribute(
        SecurityAction.Deny, 
        UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser, 
        UserQuota = 100)
     */
    private void UserQuota_UsageDemo()
    {
        Console.WriteLine("\nUserQuota_Usage demo.");
        boolean returnValue = false;
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.None);
        Console.WriteLine(
            "Checking UsageAllowed for IsolatedStorageContainment.None");
        returnValue = UsageAllowed(isfp0, IsolatedStorageContainment.None, 
            false);
        if (!(returnValue)) {
            Console.WriteLine("IsolatedStorageContainment.None demo failed");
        }
        Console.WriteLine("Checking UsageAllowed for " 
            + "IsolatedStorageContainment.DomainIsolationByRoamingUser.");
        returnValue = UsageAllowed(isfp0, 
            IsolatedStorageContainment.DomainIsolationByRoamingUser, true);
        if (!(returnValue)) {
            Console.WriteLine("IsolatedStorageContainment." 
                + "DomainIsolationByRoamingUser failed");
        }
        Console.WriteLine("Checking UsageAllowed for " 
            + "IsolatedStorageContainment.AssemblyIsolationByUser.");
        returnValue = UsageAllowed(isfp0, 
            IsolatedStorageContainment.AssemblyIsolationByUser, true);
        if (!(returnValue)) {
            Console.WriteLine(
                "IsolatedStorageContainment.AssemblyIsolationByUse failed");
        }
        Console.WriteLine("Requesting UserQuota of 0.");
        returnValue = UserQuota(isfp0, 0, true);
        if (!(returnValue)) {
            Console.WriteLine("UserQuota 0 failed");
        }
        Console.WriteLine("Requesting UserQuota of 100.");
        returnValue = UserQuota(isfp0, 100, true);
        if (!(returnValue)) {
            Console.WriteLine("UserQuota 100 failed");
        }
        Console.WriteLine("Requesting UserQuota of -2147483648.");
        returnValue = UserQuota(isfp0, -2147483648, true);
        if (!(returnValue)) {
            Console.WriteLine("UserQuota -214748364 failed");
        }
    } //UserQuota_UsageDemo   

    // IsSubsetOfDemo demonstrates the IsSubsetOf method.
    // IsSubsetOf determines whether the current permission is a subset 
    // of the specified permission.
    private void IsSubsetOfDemo()
    {
        Console.WriteLine("\nIsSubsetOf demo.");
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp1 = 
            new IsolatedStorageFilePermission(PermissionState.None);
        IsolatedStorageFilePermission isfp2 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        IsolatedStorageFilePermission isfp3 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);
        if (isfp0.IsSubsetOf(isfp1)) {
            Console.WriteLine("PermissionState.None is a subset of " 
                + "PermissionState.None.");
        }
        else {
            Console.WriteLine("IsSubsetOfDemo failed: PermissionState.None " 
                + "should be a subset of PermissionState.None");
        }
        if (isfp0.IsSubsetOf(isfp2)) {
            Console.WriteLine("PermissionState.None is a subset of " 
            + "PermissionState.Unrestricted.");
        }
        else {
            Console.WriteLine("IsSubsetOfDemo failed: PermissionState.None " 
                + "should be a subset of PermissionState.Unrestricted");
        }
        if (!(isfp2.IsSubsetOf(isfp0))) {
            Console.WriteLine("PermissionState.Unrestricted is not " 
                + "a subset of PermissionState.None.");
        }
        else {
            Console.WriteLine(("IsSubsetOfDemo failed: PermissionState." 
                + "Unrestricted should not be a subset " 
                + "\n\tof PermissionState.None"));
        }
        if (isfp2.IsSubsetOf(isfp3)) {
            Console.WriteLine("PermissionState.Unrestricted is a " 
                + "subset of PermissionState.Unrestricted.");
        }
        else {
            Console.WriteLine(("IsSubsetOfDemo failed: " 
                + "Permissionstate.Unrestricted should be a subset of " 
                + "\n\tPermissionState.Unrestricted"));
        }
    } //IsSubsetOfDemo

    // **********************************************************************
    // The following methods are simple helper functions.
    // **********************************************************************
    // **********************************************************************
    // UserQuota
    // Sets UserQuota to the value that was passed in.
    //
    private boolean UserQuota(IsolatedStorageFilePermission isfp, 
        int userQuota, boolean exceptionExpected)
    {
        boolean exceptionCaught = false;

        isfp.set_UserQuota(userQuota);
        
        try {
            isfp.Demand();
        }
        catch (System.Exception e) {
            exceptionCaught = true;
            if (!(exceptionExpected)) {
                Console.WriteLine("An unexpected exception was thrown " 
                    + "when setting UserQuota value [{0}]", 
                    System.Convert.ToString(userQuota));
                Console.WriteLine(("Exception: \r\n" + e.ToString()));
            }
        }
        if ((!(exceptionCaught)) && exceptionExpected) {
            Console.WriteLine("An expected exception not thrown " 
                + "when setting UserQuota value [{0}]", 
                System.Convert.ToString(userQuota));
        }
        return true;
    } //UserQuota

    // **********************************************************************
    // UsageAllowed
    // This method sets UsageAllowed to the value that was passed in.
    //
    private boolean UsageAllowed(IsolatedStorageFilePermission isfp, 
        IsolatedStorageContainment isc, boolean exceptionExpected)
    {
        boolean exceptionCaught = false;

        isfp.set_UsageAllowed(isc);
        
        try {
            isfp.Demand();
        }
        catch (System.Exception e) {
            exceptionCaught = true;
            if (!(exceptionExpected)) {
                Console.WriteLine("An unexpected exception was thrown " 
                    + "when setting IsolatedStorageContainment value [{0}]", 
                    isc.ToString());
                Console.WriteLine(("Exception: \r\n" + e.ToString()));
            }
        }
        if (!(exceptionCaught) && exceptionExpected) {
            Console.WriteLine("An expected exception was not thrown " 
                + "when setting IsolatedStorageContainment value [{0}]", 
                isc.ToString());
        }
        return true;
    } //UsageAllowed  

    // **********************************************************************
    // DoDemand()
    // This method demands an IsolatedStorageFilePermission.
    private int DoDemand()
    {
        int returnValue = 0;
        IsolatedStorageFilePermission isfp0 = 
            new IsolatedStorageFilePermission(PermissionState.Unrestricted);

        try {
            isfp0.Demand();
        }
        catch (SecurityException exp) {
            returnValue = 1;
        }
        catch (System.Exception e) {
            Console.WriteLine(("An unexpected exception was " 
                + "thrown in DoDemand\r\n" + e.ToString()));
            returnValue = -1;
        }
        return returnValue;
    } //DoDemand

    // **********************************************************************
    // DemandUI()
    // This method demands a UIPermission
    // Returns:
    // 0 if the demand was successful.
    // 1 if a SecurityException was thrown.
    // -1 if an unexpected exception was thrown.
    //
    private int DemandUI()
    {
        int returnValue = 0;
        UIPermission MyPermission = 
            new UIPermission(PermissionState.Unrestricted);

        try {
            MyPermission.Demand();
        }
        catch (SecurityException exp) {
            returnValue = 1;
        }
        catch (System.Exception e) {
            Console.WriteLine(("An unexpected exception was thrown " 
                + "in DemandUI\r\n" + e.ToString()));
        }
        return returnValue;
    } //DemandUI   

    public static void main(String[] args)
    {
        Demo myDemo = new Demo();
        myDemo.Run();
        return;
    } //main
} //Demo

Vererbungshierarchie

System.Object
   System.Security.CodeAccessPermission
     System.Security.Permissions.IsolatedStoragePermission
      System.Security.Permissions.IsolatedStorageFilePermission

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

IsolatedStorageFilePermission-Member
System.Security.Permissions-Namespace
IsolatedStorageFilePermissionAttribute
IsolatedStoragePermission
IsolatedStoragePermissionAttribute
IsolatedStorageContainment-Enumeration

Weitere Ressourcen

Sicherheitsberechtigungen
Anfordern von Berechtigungen