Export (0) Print
Expand All

FileDialogPermission Class

Controls the ability to access files or folders through a File dialog box. This class cannot be inherited.

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

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class FileDialogPermission : CodeAccessPermission, 
	IUnrestrictedPermission

This permission is typically used to provide limited access to user-specified files when FileIOPermission is not granted.

The following code example demonstrates the behavior of the FileDialogPermission class methods.

The example is intended to show how the methods perform if you execute the methods from your code. In general, the methods of permission classes are used by the security infrastructure; they are not typically used in applications.

For an example that shows the use of the FileDialogPermission class to demand permission for a specific FileDialogPermissionAccess enumeration value, see the FileDialogPermission(FileDialogPermissionAccess) constructor.

using System;
using System.Security;
using System.Security.Permissions;

[assembly: CLSCompliant(true)]

public class FileDialogPermissionDemo
{
    FileDialogPermission fileDialogPerm1 = new FileDialogPermission(FileDialogPermissionAccess.Open);
    FileDialogPermission fileDialogPerm2 = new FileDialogPermission(FileDialogPermissionAccess.OpenSave);
    FileDialogPermission fileDialogPerm3 = new FileDialogPermission(PermissionState.None);
    // IsSubsetOf determines whether the current permission is a subset of the 
    // specified permission. 
    private bool IsSubsetOfDemo()
    {
        bool returnValue = true;


        if (fileDialogPerm1.IsSubsetOf(fileDialogPerm2))
        {
            Console.WriteLine(fileDialogPerm1.Access + " is a subset of " +
                fileDialogPerm2.Access);
        }
        else
        {
                        Console.WriteLine(fileDialogPerm1.Access + " is not a subset of " +
                            fileDialogPerm2.Access);
                    }

        return returnValue;
    }

    // Union creates a new permission that is the union of the current permission and the 
    // specified permission. 
    private bool UnionDemo()
    {
        bool returnValue = true;

                fileDialogPerm3 = (FileDialogPermission)fileDialogPerm1.Union(fileDialogPerm2);

                if (fileDialogPerm3 == null)
                {
                    Console.WriteLine("The union of " + fileDialogPerm1.Access + 
                        " and " + fileDialogPerm2.Access + " is null.");
                }
                else
                {
                    Console.WriteLine("The union of " + fileDialogPerm1.Access + " and " +
                        fileDialogPerm2.Access + " = " +
                        ((FileDialogPermission)fileDialogPerm3).Access.ToString());
                }

        return returnValue;

    }

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

        fileDialogPerm3 = (FileDialogPermission)fileDialogPerm1.Intersect(fileDialogPerm2);
        if (fileDialogPerm3 != null)
        {
            Console.WriteLine("The intersection of " + fileDialogPerm1.Access + 
                " and " + fileDialogPerm2.Access + " = " +
                ((FileDialogPermission)fileDialogPerm3).Access.ToString());
        }
        else
        {
            Console.WriteLine("The intersection of " + fileDialogPerm1.Access + 
                " and " + fileDialogPerm2.Access + " is null.");
        }

        return returnValue;
    }

    // Copy creates and returns an identical copy of the current permission. 
    private bool CopyDemo()
    {

        bool returnValue = true;

        fileDialogPerm2 = (FileDialogPermission)fileDialogPerm1.Copy();
        Console.WriteLine("Result of copy = " + fileDialogPerm2.ToString());

        return returnValue;
    }

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

        bool returnValue = true;

        fileDialogPerm2 = new FileDialogPermission(PermissionState.None);
        fileDialogPerm2.FromXml(fileDialogPerm1.ToXml());
        Console.WriteLine("Result of ToFromXml = " +
            fileDialogPerm2.ToString());

        return returnValue;
    }

    // Invoke all demos. 
    public bool RunDemo()
    {
        bool returnCode = true;
        bool tempReturnCode;

        // Call IsSubsetOf demo. 
        if (tempReturnCode = IsSubsetOfDemo()) Console.Out.WriteLine("IsSubsetOf demo completed successfully.");
        else Console.Out.WriteLine("IsSubsetOf demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call Union demo. 
        if (tempReturnCode = UnionDemo()) Console.Out.WriteLine("Union demo completed successfully.");
        else Console.Out.WriteLine("Union demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call Intersect demo. 
        if (tempReturnCode = IntersectDemo()) Console.Out.WriteLine("Intersect demo completed successfully.");
        else Console.Out.WriteLine("Intersect demo failed.");
        returnCode = tempReturnCode && returnCode;


        // Call Copy demo. 
        if (tempReturnCode = CopyDemo()) Console.Out.WriteLine("Copy demo completed successfully.");
        else Console.Out.WriteLine("Copy demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call ToFromXml demo. 
        if (tempReturnCode = ToFromXmlDemo()) Console.Out.WriteLine("ToFromXml demo completed successfully.");
        else Console.Out.WriteLine("ToFromXml demo failed.");
        returnCode = tempReturnCode && returnCode;

        return (returnCode);

    }
    // Test harness. 
    public static void Main(String[] args)
    {
        try
        {
            FileDialogPermissionDemo democase = new FileDialogPermissionDemo();
            bool returnCode = democase.RunDemo();
            if (returnCode)
            {
                Console.Out.WriteLine("FileDialogPermission demo completed successfully.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 100;
            }
            else
            {
                Console.Out.WriteLine("FileDialogPermission demo failed.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 101;
            }
        }
        catch (Exception e)
        {
            Console.Out.WriteLine("FileDialogPermission failed.");
            Console.WriteLine(e.ToString());
            Console.Out.WriteLine("Press the Enter key to exit.");
            string consoleInput = Console.ReadLine();
            System.Environment.ExitCode = 101;
        }
    }
}

System.Object
  System.Security.CodeAccessPermission
    System.Security.Permissions.FileDialogPermission

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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft