Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

IsolatedStorageFilePermission Class

Specifies the allowed usage of a private virtual file system. This class cannot be inherited.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class IsolatedStorageFilePermission : IsolatedStoragePermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class IsolatedStorageFilePermission extends IsolatedStoragePermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class IsolatedStorageFilePermission extends IsolatedStoragePermission

The common language runtime (CLR) uses this class to control access to isolated storage.

Isolated storage creates a unique storage area for use by an application or component. It provides true isolation in that the identity of an application uniquely determines the root of a virtual file system, which only that application can access. Thus, each application has its own file area automatically assigned to it. This file area is fully isolated from other applications, making it private to that application.

NoteNote

There is no effect if you use Assert, PermitOnly, or Deny to add stack modifiers for usage or quota. Usage and quota are determined from evidence and a stack walk is not performed for demands, making the above operations ineffective.

The following example shows the use of members of the IsolatedStorageFilePermission class.

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 ;
    }
}

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

System.Object
   System.Security.CodeAccessPermission
     System.Security.Permissions.IsolatedStoragePermission
      System.Security.Permissions.IsolatedStorageFilePermission
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 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

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

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.