Export (0) Print
Expand All

FileDialogPermission Class

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

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

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class FileDialogPermission
	Inherits CodeAccessPermission
	Implements IUnrestrictedPermission
'Usage
Dim instance As FileDialogPermission

/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class FileDialogPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class FileDialogPermission extends CodeAccessPermission implements 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 the results of the methods, not to show how the methods are typically used. For an example showing the use of the FileDialogPermission class to demand permission for a specific FileDialogPermissionAccess enumeration value, see the FileDialogPermission(FileDialogPermissionAccess) constructor.

Imports System
Imports System.Security
Imports System.Security.Permissions



<assembly: CLSCompliant(True)>
 

Public Class FileDialogPermissionDemo
    Private fileDialogPerm1 As New FileDialogPermission(FileDialogPermissionAccess.Open)
    Private fileDialogPerm2 As New FileDialogPermission(FileDialogPermissionAccess.OpenSave)
    Private fileDialogPerm3 As New FileDialogPermission(PermissionState.None)
    
    ' IsSubsetOf determines whether the current permission is a subset of the
    ' specified permission.
    Private Function IsSubsetOfDemo() As Boolean 
        Dim returnValue As Boolean = True
        
        
        If fileDialogPerm1.IsSubsetOf(fileDialogPerm2) Then
            Console.WriteLine(fileDialogPerm1.Access.ToString() + " is a subset of " _
            + fileDialogPerm2.Access.ToString())
        Else
            Console.WriteLine(fileDialogPerm1.Access.ToString + " is not a subset of " + _
            fileDialogPerm2.Access.ToString())
        End If
        
        Return returnValue
    
    End Function 'IsSubsetOfDemo
    
    ' Union creates a new permission that is the union of the current permission and the
    ' specified permission.
    Private Function UnionDemo() As Boolean 
        Dim returnValue As Boolean = True
        
        fileDialogPerm3 = CType(fileDialogPerm1.Union(fileDialogPerm2), FileDialogPermission)
        
        If fileDialogPerm3 Is Nothing Then
            Console.WriteLine("The union of " + fileDialogPerm1.Access.ToString() + _
            " and " + fileDialogPerm2.Access.ToString() + " is null.")
        Else
            Console.WriteLine("The union of " + fileDialogPerm1.Access.ToString() + _
            " and " + fileDialogPerm2.Access.ToString() + " = " + _
            CType(fileDialogPerm3, FileDialogPermission).Access.ToString())
        End If
        
        Return returnValue
    
    End Function 'UnionDemo
     
    ' Intersect creates and returns a new permission that is the intersection of the current
    ' permission and the permission specified.
    Private Function IntersectDemo() As Boolean 
        Dim returnValue As Boolean = True
        
        fileDialogPerm3 = CType(fileDialogPerm1.Intersect(fileDialogPerm2), FileDialogPermission)
        If Not (fileDialogPerm3 Is Nothing) Then
            Console.WriteLine("The intersection of " + fileDialogPerm1.Access.ToString() + " and " _
            + fileDialogPerm2.Access.ToString() + " = " + CType(fileDialogPerm3, _
            FileDialogPermission).Access.ToString())
        Else
            Console.WriteLine("The intersection of " + fileDialogPerm1.Access.ToString() + " and " + _
            fileDialogPerm2.Access + " is null.")
        End If
        
        Return returnValue
    
    End Function 'IntersectDemo
    
    ' Copy creates and returns an identical copy of the current permission.
    Private Function CopyDemo() As Boolean 
        
        Dim returnValue As Boolean = True
        
        fileDialogPerm2 = CType(fileDialogPerm1.Copy(), FileDialogPermission)
        Console.WriteLine("Result of copy = " + fileDialogPerm2.ToString())
        
        Return returnValue
    
    End Function '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 Function ToFromXmlDemo() As Boolean 
        
        Dim returnValue As Boolean = True
        
        fileDialogPerm2 = New FileDialogPermission(PermissionState.None)
        fileDialogPerm2.FromXml(fileDialogPerm1.ToXml())
        Console.WriteLine("Result of ToFromXml = " + fileDialogPerm2.ToString())
        
        Return returnValue
    
    End Function 'ToFromXmlDemo
    
    ' Invoke all demos.
    Public Function RunDemo() As Boolean
        Dim returnCode As Boolean = True
        Dim tempReturnCode As Boolean

        ' Call the IsSubsetOf demo.
        If IsSubsetOfDemo() Then
            Console.Out.WriteLine("IsSubsetOf demo completed successfully.")
            tempReturnCode = True
        Else
            Console.Out.WriteLine("IsSubsetOf demo failed.")
            tempReturnCode = False
        End If
        returnCode = tempReturnCode And returnCode

        ' Call the Union demo.
        If UnionDemo() Then
            Console.Out.WriteLine("Union demo completed successfully.")
            tempReturnCode = True
        Else
            Console.Out.WriteLine("Union demo failed.")
            tempReturnCode = False
        End If
        returnCode = tempReturnCode And returnCode

        ' Call the Intersect demo.	
        If IntersectDemo() Then
            Console.Out.WriteLine("Intersect demo completed successfully.")
            tempReturnCode = True
        Else
            Console.Out.WriteLine("Intersect demo failed.")
            tempReturnCode = False
        End If
        returnCode = tempReturnCode And returnCode


        ' Call the Copy demo.	
        If CopyDemo() Then
            Console.Out.WriteLine("Copy demo completed successfully.")
            tempReturnCode = True
        Else
            Console.Out.WriteLine("Copy demo failed.")
            tempReturnCode = False
        End If
        returnCode = tempReturnCode And returnCode

        ' Call the ToFromXml demo.	
        If ToFromXmlDemo() Then
            Console.Out.WriteLine("ToFromXml demo completed successfully.")
            tempReturnCode = True
        Else
            Console.Out.WriteLine("ToFromXml demo failed.")
            tempReturnCode = False
        End If
        returnCode = tempReturnCode And returnCode

        Return returnCode
    End Function
     
    ' Test harness.
    Public Shared Sub Main(ByVal args() As String) 
        Try
            Dim democase As New FileDialogPermissionDemo()
            Dim returnCode As Boolean = democase.RunDemo()
            If returnCode Then
                Console.Out.WriteLine("FileDialogPermission demo completed successfully.")
                Console.Out.WriteLine("Press the Enter key to exit.")
                Dim consoleInput As String = Console.ReadLine()
                System.Environment.ExitCode = 100
            Else
                Console.Out.WriteLine("FileDialogPermission demo failed.")
                Console.Out.WriteLine("Press the Enter key to exit.")
                Dim consoleInput As String = Console.ReadLine()
                System.Environment.ExitCode = 101
            End If
        Catch e As Exception
            Console.Out.WriteLine("FileDialogPermission failed.")
            Console.WriteLine(e.ToString())
            Console.Out.WriteLine("Press the Enter key to exit.")
            Dim consoleInput As String = Console.ReadLine()
            System.Environment.ExitCode = 101
        End Try
    
    End Sub 'Main
End Class 'FileDialogPermissionDemo

// 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 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

Community Additions

ADD
Show:
© 2015 Microsoft