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
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class FileDialogPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class FileDialogPermission extends CodeAccessPermission implements IUnrestrictedPermission
Not applicable.

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

// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, 
// ToXml and FromXml methods
// of the FileDialogPermission class.
import System .* ;
import System.Security .* ;
import System.Security.Permissions .* ;

/** @assembly CLSCompliant(true)
 */

public class FileDialogPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a subset of the
    // specified permission.
    private boolean IsSubsetOfDemo() 
    {
        boolean returnValue = true;
        FileDialogPermissionAccess fileDialog1[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermissionAccess fileDialog2[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermission fileDialogIdPerm1[] = 
            new FileDialogPermission[1];
        FileDialogPermission fileDialogIdPerm2[] = 
            new FileDialogPermission[1];
        FileDialogGenerator fileDialogGen1 =  
            new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 =  
            new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog
            (fileDialogIdPerm1, fileDialog1)) {
            if (fileDialogIdPerm1[0] == null) {
                continue ;
            }
            fileDialogGen2.ResetIndex();
            Console.WriteLine("**********************" 
                + "**********************************\n");
            
            while(fileDialogGen2.CreateFileDialog(
                fileDialogIdPerm2, fileDialog2)) {
                if (fileDialogIdPerm2[0] == null) {
                    continue ;
                }
                try {
                    if (fileDialogIdPerm1[0].
                        IsSubsetOf(fileDialogIdPerm2[0])) {
                        Console.WriteLine((fileDialogIdPerm1[0].get_Access() 
                            + " is a subset of " 
                            + fileDialogIdPerm2[0].get_Access()));
                    }
                    else {
                        Console.WriteLine((fileDialogIdPerm1[0].get_Access() 
                            + " is not a subset of " + 
                        fileDialogIdPerm2[0].get_Access()));
                    }
                }
                catch(System.Exception  e){
                    Console.WriteLine(("An exception was thrown " 
                        + "for subset :" + fileDialogIdPerm1[0] 
                        + "\n" + fileDialogIdPerm2[0] + "\n" + e));
                        returnValue = false;
                }
            }
        }
        return returnValue ;
    } //IsSubsetOfDemo

    // Union creates a new permission that is the union of 
    // the current permission and the
    // specified permission.
    private boolean UnionDemo() 
    {
        boolean returnValue = true;
        FileDialogPermissionAccess fileDialog1[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermissionAccess fileDialog2[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermission fileDialogIdPerm1[] = 
            new FileDialogPermission[1];
        FileDialogPermission fileDialogIdPerm2[] = 
            new FileDialogPermission[1];
        IPermission fileDialogIdPerm3;
        FileDialogGenerator fileDialogGen1 =  
            new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 =  
            new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, 
            fileDialog1)) {
            if (fileDialogIdPerm1[0] == null) {
                continue ;
            }
            Console.WriteLine("************************" 
                + "**********************************\n");
            fileDialogGen2.ResetIndex();
            
            while(fileDialogGen2.CreateFileDialog(fileDialogIdPerm2, 
                fileDialog2)) {
                if (fileDialogIdPerm2[0] == null) {
                    continue ;
                }
                fileDialogIdPerm3 =((FileDialogPermission)(
                    fileDialogIdPerm1[0].Union(fileDialogIdPerm2[0])));
                fileDialogIdPerm3 = fileDialogIdPerm1[0].
                    Union(fileDialogIdPerm2[0]);
                
                if (fileDialogIdPerm3 == null) {
                    Console.WriteLine(("The union of " + fileDialog1[0] 
                        + " and " + fileDialog2[0] + " is null."));
                }
                else {
                    Console.WriteLine(("The union of " 
                        + fileDialogIdPerm1[0].get_Access() + " and " 
                        + fileDialogIdPerm2[0].get_Access() + " = " 
                        + ((FileDialogPermission)(fileDialogIdPerm3)).
                        get_Access().ToString()));
                }
            }
        }
        return returnValue ;
    } //UnionDemo

    // Intersect creates and returns a new permission that is the 
    // intersection of the current
    // permission and the permission specified.
    private boolean IntersectDemo() 
    {
        boolean returnValue = true;
        FileDialogPermissionAccess fileDialog1[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermissionAccess fileDialog2[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermission fileDialogIdPerm1[] = 
            new FileDialogPermission[1];
        FileDialogPermission fileDialogIdPerm2[] = 
            new FileDialogPermission[1];
        FileDialogPermission fileDialogIdPerm3 = null;
        FileDialogGenerator fileDialogGen1 =  
            new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 =  
            new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(
            fileDialogIdPerm1, fileDialog1)) {
            if (fileDialogIdPerm1[0] == null) {
                continue ;
            }
            Console.WriteLine("***************************" 
                + "*******************************\n");
            fileDialogGen2.ResetIndex();
            
            while(fileDialogGen2.CreateFileDialog(
                fileDialogIdPerm2, fileDialog2)) {
                if (fileDialogIdPerm2[0] == null) {
                    continue ;
                }
                
                fileDialogIdPerm3 =((FileDialogPermission)
                    (fileDialogIdPerm1[0].Intersect
                    (fileDialogIdPerm2[0])));
                if (fileDialogIdPerm3  != null) {
                    Console.WriteLine(("The intersection of " 
                        + fileDialog1[0] + " and " + fileDialog2[0] 
                        + " = " +((FileDialogPermission)(fileDialogIdPerm3)).
                        get_Access().ToString()));
                }
                else {
                    Console.WriteLine(("The intersection of " 
                        + fileDialog1[0] + " and " + fileDialog2[0] 
                        + " is null."));
                }
            }
        }
        return returnValue ;
    } //IntersectDemo

    // Copy creates and returns an identical copy of the current permission.
    private boolean CopyDemo() 
    {
        boolean returnValue = true;
        FileDialogPermissionAccess fileDialog1[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermission fileDialogIdPerm1[] = 
            new FileDialogPermission[1];
        FileDialogPermission fileDialogIdPerm2[] = 
            new FileDialogPermission[1];
        FileDialogGenerator fileDialogGen1 =  
            new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 =  
            new FileDialogGenerator();

        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, 
            fileDialog1)) {
            if ( fileDialogIdPerm1[0] == null  ) {
                continue ;
            }
            fileDialogGen2.ResetIndex();
            Console.WriteLine("************************" 
                + "********************************\n");
            try {
                fileDialogIdPerm2[0] =(
                    (FileDialogPermission)(fileDialogIdPerm1[0].Copy()));
                Console.WriteLine(("Result of copy = " 
                    + fileDialogIdPerm2[0].ToString()));
            } 
            catch(System.Exception  e) {
                Console.WriteLine(("Copy failed :" 
                    + fileDialogIdPerm1[0].ToString() + e));
                continue ;
            }
        }
        return returnValue ;
    } //CopyDemo   

    // ToXml creates an XML encoding of the permission and its current 
    // state; FromXml reconstructs a
    // permission with the specified state from the XML encoding.
    private boolean ToFromXmlDemo() 
    {
        boolean returnValue = true;      
        FileDialogPermissionAccess fileDialog1[] = 
            new FileDialogPermissionAccess[1];
        FileDialogPermission fileDialogIdPerm1[] = 
            new FileDialogPermission[1];
        FileDialogPermission fileDialogIdPerm2[] = 
            new FileDialogPermission[1];
        FileDialogGenerator fileDialogGen1 =  
            new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 =  
            new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, 
            fileDialog1)) {
            if (fileDialogIdPerm1[0] == null) {
                continue ;
            }
            Console.WriteLine("************************" 
                + "********************************\n");
            fileDialogGen2.ResetIndex();
            try {
                fileDialogIdPerm2[0] = 
                    new FileDialogPermission(PermissionState.None);
                fileDialogIdPerm2[0].FromXml(fileDialogIdPerm1[0].ToXml());
                Console.WriteLine(("Result of ToFromXml = " 
                    + fileDialogIdPerm2[0].ToString()));
            }
            catch(System.Exception  e) {
                Console.WriteLine(("ToFromXml failed :" 
                    + fileDialogIdPerm1[0].ToString() + e));
                continue ;
            }
        }
        return returnValue ;
    } //ToFromXmlDemo    

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

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

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

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

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

        // Call ToFromXml demo.
        if (tempReturnCode = ToFromXmlDemo()) {
            Console.get_Out().WriteLine("ToFromXml demo completed " 
                + "successfully.");
        }
        else {
            Console.get_Out().WriteLine("ToFromXml demo failed.");
        }
        returnCode = tempReturnCode && returnCode;
        return returnCode ;
    } //RunDemo   
 
    // Test harness.
    public static void main(String[] args)
    {
        try {
            FileDialogPermissionDemo democase =  
                new FileDialogPermissionDemo();
            boolean returnCode = democase.RunDemo();
            if (returnCode) {
                Console.get_Out().WriteLine("FileDialogPermission demo " 
                    + "completed successfully.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                System.Environment.set_ExitCode(100);
            }
            else {
                Console.get_Out().WriteLine("FileDialogPermission demo " 
                    + "failed.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                System.Environment.set_ExitCode(101);
            }
        }
        catch(System.Exception  e){
            Console.get_Out().WriteLine("FileDialogPermission failed.");
            Console.WriteLine(e.ToString());
            Console.get_Out().WriteLine("Press the Enter key to exit.");
            String consoleInput = Console.ReadLine();
            System.Environment.set_ExitCode(101);
        }
    } //main
} //FileDialogPermissionDemo

// This class generates FileDialogPermission objects.
class FileDialogGenerator
{
    private FileDialogPermissionAccess myFileDialog[] =  
        {FileDialogPermissionAccess.None, 
            FileDialogPermissionAccess.Open, 
            FileDialogPermissionAccess.OpenSave, 
            FileDialogPermissionAccess.Save};   
    private int fileDialogIndex = 0;   

    public FileDialogGenerator() 
    {
        ResetIndex();
    } //FileDialogGenerator

    public void ResetIndex() 
    {
        fileDialogIndex = 0;
    } //ResetIndex   

    // Create a FileDialogPermission.
    public boolean CreateFileDialog(FileDialogPermission fileDialogPerm[], 
        FileDialogPermissionAccess fileDialog[]) 
    {
        if (fileDialogIndex >= myFileDialog.length) {
            fileDialogPerm[0] = new FileDialogPermission(PermissionState.None);
            fileDialog[0] = FileDialogPermissionAccess.None;
            return false ;
        }
        fileDialog[0] = myFileDialog[fileDialogIndex++];
        try {
            fileDialogPerm[0] = new FileDialogPermission(fileDialog[0]);
            return true ;
        }
        catch(System.Exception  e) {
            Console.WriteLine(("Cannot create FileDialogPermission: " 
                + fileDialog + " " + e));
            fileDialogPerm[0] = new FileDialogPermission(PermissionState.None);
            fileDialog[0] = FileDialogPermissionAccess.None;
            return true ;
        }
    } //CreateFileDialog
} //FileDialogGenerator

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 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft