//</Snippet3>

    // Intersect creates and returns a new permission that is the intersection of the current
    // permission and the permission specified.
    //<Snippet4>
    private bool IntersectDemo()
    {
        bool returnValue = true;

        string           fileIO1, fileIO2;
        FileIOPermission fileIOPerm1, fileIOPerm2, fileIOPerm3;

        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath(out fileIO1))
        {
            if (fileIO1 == "")
            {
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            }
            else
            {
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO1);
            }

            Console.WriteLine("**********************************************************\n");
            fileIOGen2.ResetIndex();

            while (fileIOGen2.CreateFilePath(out fileIO2))
            {
                if (fileIO2 == "")
                {
                    fileIOPerm2 = new FileIOPermission(PermissionState.None);
                }
                else
                {
                    fileIOPerm2 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO2);
                }
                string firstPermission  = fileIO1 == "" | fileIO1 == null ? "null" : fileIO1;
                string secondPermission = fileIO2 == "" | fileIO2 == null ? "null" : fileIO2;
                try
                {
                    fileIOPerm3 = (FileIOPermission)fileIOPerm1.Intersect(fileIOPerm2);
                    if (fileIOPerm3 != null && fileIOPerm3.GetPathList(FileIOPermissionAccess.Read) != null)
                    {
                        Console.WriteLine("The intersection of " + firstPermission + " and \n\t" + secondPermission +
                                          " = \n\t" + ((FileIOPermission)fileIOPerm3).GetPathList(FileIOPermissionAccess.Read)[0]);
                    }
                    else
                    {
                        Console.WriteLine("The intersection of " + firstPermission + " and " + secondPermission + " is null.");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for intersection " + e);
                    returnValue = false;
                }
            }
        }

        return(returnValue);
    }
    //</Snippet4>

    //Copy creates and returns an identical copy of the current permission.
    //<Snippet5>
    private bool CopyDemo()
    {
        bool             returnValue = true;
        string           fileIO1;
        FileIOPermission fileIOPerm1, fileIOPerm2;
        FileIOGenerator  fileIOGen1 = new FileIOGenerator();
        FileIOGenerator  fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath(out fileIO1))
        {
            if (fileIO1 == "")
            {
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            }
            else
            {
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO1);
            }

            Console.WriteLine("**********************************************************\n");
            fileIOGen2.ResetIndex();
            try
            {
                fileIOPerm2 = (FileIOPermission)fileIOPerm1.Copy();
                if (fileIOPerm2 != null)
                {
                    Console.WriteLine("Result of copy = " + fileIOPerm2.ToString() + "\n");
                }
                else
                {
                    Console.WriteLine("Result of copy is null. \n");
                }
            }
            catch (Exception e)
            {
                {
                    if (fileIO1 == "")
                    {
                        Console.WriteLine("The target FileIOPermission is empty, copy failed.");
                    }
                    else
                    {
                        Console.WriteLine(e);
                    }
                }
                continue;
            }
        }
        return(returnValue);
    }
    //</Snippet5>

    // ToXml creates an XML encoding of the permission and its current state;
    // FromXml reconstructs a permission with the specified state from the XML encoding.
    //<Snippet6>
    private bool ToFromXmlDemo()
    {
        bool returnValue = true;

        string           fileIO1;
        FileIOPermission fileIOPerm1, fileIOPerm2;

        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath(out fileIO1))
        {
            if (fileIO1 == "")
            {
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            }
            else
            {
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.Read, fileIO1);
            }

            Console.WriteLine("********************************************************\n");
            fileIOGen2.ResetIndex();
            try
            {
                fileIOPerm2 = new FileIOPermission(PermissionState.None);
                fileIOPerm2.FromXml(fileIOPerm1.ToXml());
                Console.WriteLine("Result of ToFromXml = " + fileIOPerm2.ToString() + "\n");
            }
            catch (Exception e)
            {
                Console.WriteLine("ToFromXml failed :" + fileIOPerm1.ToString() + e);
                continue;
            }
        }

        return(returnValue);
    }
    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    // This method compares various FileIOPermission paths with FileIOPermissionAccess set to AllAccess.
    //<Snippet2>
    private bool IsSubsetOfDemo()
    {
        bool returnValue = true;

        string           fileIO1, fileIO2;
        FileIOPermission fileIOPerm1, fileIOPerm2;

        FileIOGenerator fileIOGen1 = new FileIOGenerator();
        FileIOGenerator fileIOGen2 = new FileIOGenerator();

        fileIOGen1.ResetIndex();
        while (fileIOGen1.CreateFilePath(out fileIO1))
        {
            if (fileIO1 == "")
            {
                fileIOPerm1 = new FileIOPermission(PermissionState.None);
            }
            else
            {
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileIO1);
            }

            Console.WriteLine("**********************************************************\n");

            fileIOGen2.ResetIndex();

            while (fileIOGen2.CreateFilePath(out fileIO2))
            {
                if (fileIO2 == "")
                {
                    fileIOPerm2 = new FileIOPermission(PermissionState.None);
                }
                else
                {
                    fileIOPerm2 = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileIO2);
                }
                string firstPermission  = fileIO1 == "" | fileIO1 == null ? "null" : fileIO1;
                string secondPermission = fileIO2 == "" | fileIO2 == null ? "null" : fileIO2;
                if (fileIOPerm2 == null)
                {
                    continue;
                }
                try
                {
                    if (fileIOPerm1.IsSubsetOf(fileIOPerm2))
                    {
                        Console.WriteLine(firstPermission + " is a subset of " + secondPermission + "\n");
                    }
                    else
                    {
                        Console.WriteLine(firstPermission + " is not a subset of " + secondPermission + "\n");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown for subset :" + fileIO1 == "" ? "null" : fileIO1 + "\n" +
                                      fileIO2 == "" ? "null" : fileIO2 + "\n" + e);
                }
            }
        }
        return(returnValue);
    }