Export (0) Print
Expand All

FileDialogPermission Class

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

For a list of all members of this type, see FileDialogPermission Members.

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

[Visual Basic]
<Serializable>
NotInheritable Public Class FileDialogPermission
   Inherits CodeAccessPermission
   Implements IUnrestrictedPermission
[C#]
[Serializable]
public sealed class FileDialogPermission : CodeAccessPermission,
   IUnrestrictedPermission
[C++]
[Serializable]
public __gc __sealed class FileDialogPermission : public
   CodeAccessPermission, IUnrestrictedPermission
[JScript]
public
   Serializable
class FileDialogPermission extends CodeAccessPermission
   implements IUnrestrictedPermission

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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

Example

[Visual Basic] 
' This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
' of the FileDialogPermission class.
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports Microsoft.VisualBasic

<Assembly: CLSCompliant(True)> 

Public Class FileDialogPermissionDemo

    ' IsSubsetOf determines whether the current permission is a subset of the 
    ' specified permission.
    Private Function IsSubsetOfDemo() As Boolean
        Dim returnValue As Boolean = True
        Dim fileDialog1, fileDialog2 As FileDialogPermissionAccess
        Dim fileDialogIdPerm1, fileDialogIdPerm2 As FileDialogPermission
        Dim fileDialogGen1 As New FileDialogGenerator()
        Dim fileDialogGen2 As New FileDialogGenerator()
        fileDialogGen1.ResetIndex()
        While fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, fileDialog1)
            If fileDialogIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            fileDialogGen2.ResetIndex()
            Console.WriteLine("********************************************************" & ControlChars.Lf)

            While fileDialogGen2.CreateFileDialog(fileDialogIdPerm2, fileDialog2)
                If fileDialogIdPerm2 Is Nothing Then
                    GoTo ContinueWhile2
                End If
                Try
                    If fileDialogIdPerm1.IsSubsetOf(fileDialogIdPerm2) Then
                        Console.WriteLine((fileDialogIdPerm1.Access.ToString() & " is a subset of " & fileDialogIdPerm2.Access.ToString()))
                    Else
                        Console.WriteLine((fileDialogIdPerm1.Access.ToString() & " is not a subset of " & fileDialogIdPerm2.Access.ToString()))
                    End If
                Catch e As Exception
                    Console.WriteLine(("An exception was thrown for subset :" & fileDialog1 & ControlChars.Lf & fileDialog2 & ControlChars.Lf & e.ToString()))
                    returnValue = False
                End Try
ContinueWhile2:
            End While
ContinueWhile1:
        End While
        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
        Dim fileDialog1, fileDialog2 As FileDialogPermissionAccess
        Dim fileDialogIdPerm1, fileDialogIdPerm2 As FileDialogPermission
        Dim fileDialogIdPerm3 As IPermission
        Dim fileDialogGen1 As New FileDialogGenerator()
        Dim fileDialogGen2 As New FileDialogGenerator()
        fileDialogGen1.ResetIndex()
        While fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, fileDialog1)
            If fileDialogIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            Console.WriteLine("**********************************************************" & ControlChars.Lf)
            fileDialogGen2.ResetIndex()

            While fileDialogGen2.CreateFileDialog(fileDialogIdPerm2, fileDialog2)
                If fileDialogIdPerm2 Is Nothing Then
                    GoTo ContinueWhile2
                End If
                fileDialogIdPerm3 = CType(fileDialogIdPerm1.Union(fileDialogIdPerm2), FileDialogPermission)
                fileDialogIdPerm3 = fileDialogIdPerm1.Union(fileDialogIdPerm2)

                If fileDialogIdPerm3 Is Nothing Then
                    Console.WriteLine(("The union of " & fileDialog1.ToString() & " and " & fileDialog2.ToString() & " is null."))
                Else
                    Console.WriteLine(("The union of " & fileDialogIdPerm1.Access.ToString() & " and " & fileDialogIdPerm2.Access.ToString() & " = " & CType(fileDialogIdPerm3, FileDialogPermission).Access.ToString()))
                End If
ContinueWhile2:
            End While
ContinueWhile1:
        End While


        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
        Dim fileDialog1, fileDialog2 As FileDialogPermissionAccess
        Dim fileDialogIdPerm1, fileDialogIdPerm2, fileDialogIdPerm3 As FileDialogPermission
        Dim fileDialogGen1 As New FileDialogGenerator()
        Dim fileDialogGen2 As New FileDialogGenerator()
        fileDialogGen1.ResetIndex()
        While fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, fileDialog1)
            If fileDialogIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            Console.WriteLine("**********************************************************" & ControlChars.Lf)
            fileDialogGen2.ResetIndex()

            While fileDialogGen2.CreateFileDialog(fileDialogIdPerm2, fileDialog2)
                If fileDialogIdPerm2 Is Nothing Then
                    GoTo ContinueWhile2
                End If 'Console.WriteLine("\tfileDialogIdPerm2 = " &fileDialogIdPerm2.ToString());
                fileDialogIdPerm3 = CType(fileDialogIdPerm1.Intersect(fileDialogIdPerm2), FileDialogPermission)
                If Not (fileDialogIdPerm3 Is Nothing) Then
                    Console.WriteLine(("The intersection of " & fileDialog1.ToString() & " and " & fileDialog2.ToString() & " = " & CType(fileDialogIdPerm3, FileDialogPermission).Access.ToString()))
                Else
                    Console.WriteLine(("The intersection of " & fileDialog1.ToString() & " and " & fileDialog2.ToString() & " is null."))
                End If
ContinueWhile2:
            End While
ContinueWhile1:
        End While

        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

        Dim fileDialog1 As FileDialogPermissionAccess
        Dim fileDialogIdPerm1, fileDialogIdPerm2 As FileDialogPermission

        Dim fileDialogGen1 As New FileDialogGenerator()
        Dim fileDialogGen2 As New FileDialogGenerator()

        fileDialogGen1.ResetIndex()
        While fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, fileDialog1)
            If fileDialogIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            fileDialogGen2.ResetIndex()
            Console.WriteLine("********************************************************" & ControlChars.Lf)
            Try
                fileDialogIdPerm2 = CType(fileDialogIdPerm1.Copy(), FileDialogPermission)
                Console.WriteLine(("Result of copy = " & fileDialogIdPerm2.ToString()))

            Catch e As Exception
                Console.WriteLine(("Copy failed :" & fileDialogIdPerm1.ToString() & e.ToString()))
                GoTo ContinueWhile1
            End Try
ContinueWhile1:
        End While
        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

        Dim fileDialog1 As FileDialogPermissionAccess
        Dim fileDialogIdPerm1, fileDialogIdPerm2 As FileDialogPermission
        Dim fileDialogGen1 As New FileDialogGenerator()
        Dim fileDialogGen2 As New FileDialogGenerator()
        fileDialogGen1.ResetIndex()
        While fileDialogGen1.CreateFileDialog(fileDialogIdPerm1, fileDialog1)
            If fileDialogIdPerm1 Is Nothing Then
                GoTo ContinueWhile1
            End If
            Console.WriteLine("********************************************************" & ControlChars.Lf)
            fileDialogGen2.ResetIndex()
            Try
                fileDialogIdPerm2 = New FileDialogPermission(PermissionState.None)
                fileDialogIdPerm2.FromXml(fileDialogIdPerm1.ToXml())
                Console.WriteLine(("Result of ToFromXml = " & fileDialogIdPerm2.ToString()))

            Catch e As Exception
                Console.WriteLine(("ToFromXml failed :" & fileDialogIdPerm1.ToString() & e.ToString()))
                GoTo ContinueWhile1
            End Try
ContinueWhile1:
        End While

        Return returnValue
    End Function 'ToFromXmlDemo

    ' Invoke all demos.
    Public Function RunDemo() As Boolean
        Dim returnCode As Boolean = True
        Dim tempReturnCode As Boolean

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

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

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


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

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

        Return returnCode
    End Function 'RunDemo

    ' Test harness.
    Public Overloads 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 class generates FileDialogPermission objects.

Friend Class FileDialogGenerator


    Private myFileDialog As FileDialogPermissionAccess() = {FileDialogPermissionAccess.None, FileDialogPermissionAccess.Open, FileDialogPermissionAccess.OpenSave, FileDialogPermissionAccess.Save}

    Private fileDialogIndex As Integer = 0


    Public Sub New()
        ResetIndex()
    End Sub 'New


    Public Sub ResetIndex()
        fileDialogIndex = 0
    End Sub 'ResetIndex

    ' Create a FileDialogPermission.
    Public Function CreateFileDialog(ByRef fileDialogPerm As FileDialogPermission, ByRef fileDialog As FileDialogPermissionAccess) As Boolean

        If fileDialogIndex >= myFileDialog.Length Then
            fileDialogPerm = New FileDialogPermission(PermissionState.None)
            fileDialog = FileDialogPermissionAccess.None
            Return False
        End If
        fileDialog = myFileDialog(fileDialogIndex)
        fileDialogIndex = fileDialogIndex + 1
        Try
            fileDialogPerm = New FileDialogPermission(fileDialog)
            Return True
        Catch e As Exception
            Console.WriteLine(("Cannot create FileDialogPermission: " & fileDialog & " " & e.ToString()))
            fileDialogPerm = New FileDialogPermission(PermissionState.None)
            fileDialog = FileDialogPermissionAccess.None
            Return True
        End Try
    End Function 'CreateFileDialog
' End of FileDialogGenerator.

[C#] 
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
// of the FileDialogPermission class.

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

[assembly:CLSCompliant(true)]

public class FileDialogPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a subset of the 
    // specified permission.
    private bool IsSubsetOfDemo()
    {
        bool returnValue = true;
        FileDialogPermissionAccess fileDialog1,fileDialog2;
        FileDialogPermission fileDialogIdPerm1,fileDialogIdPerm2;
        FileDialogGenerator fileDialogGen1 = new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 = new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(out fileDialogIdPerm1, out fileDialog1)) 
        {
            if(fileDialogIdPerm1 == null) continue;
            fileDialogGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");

            while(fileDialogGen2.CreateFileDialog(out fileDialogIdPerm2, out fileDialog2)) 
            {
                if(fileDialogIdPerm2 == null) continue;
                try
                {            
                    if (fileDialogIdPerm1.IsSubsetOf(fileDialogIdPerm2))
                    {
                        Console.WriteLine(fileDialogIdPerm1.Access + " is a subset of " + 
                            fileDialogIdPerm2.Access);
                    }
                    else
                    {
                        Console.WriteLine(fileDialogIdPerm1.Access + " is not a subset of " + 
                            fileDialogIdPerm2.Access);
                    }
                }
                catch(Exception e) 
                {
                    Console.WriteLine("An exception was thrown for subset :" + fileDialogIdPerm1 + "\n" + 
                        fileDialogIdPerm2 +"\n" + e);
                    returnValue=false;
                }
            }
        }
        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;
        FileDialogPermissionAccess fileDialog1,fileDialog2;
        FileDialogPermission fileDialogIdPerm1,fileDialogIdPerm2;
        IPermission fileDialogIdPerm3;
        FileDialogGenerator fileDialogGen1 = new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 = new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(out fileDialogIdPerm1, out fileDialog1)) 
        {
            if(fileDialogIdPerm1 == null) continue;
            Console.WriteLine("**********************************************************\n");
            fileDialogGen2.ResetIndex();

            while(fileDialogGen2.CreateFileDialog(out fileDialogIdPerm2, out fileDialog2)) 
            {
                if(fileDialogIdPerm2 == null) continue;
                fileDialogIdPerm3 = (FileDialogPermission)fileDialogIdPerm1.Union(fileDialogIdPerm2);
                fileDialogIdPerm3 = fileDialogIdPerm1.Union(fileDialogIdPerm2);

                if(fileDialogIdPerm3 == null)  
                {
                    Console.WriteLine("The union of " +  fileDialog1 + " and " + fileDialog2 + " is null." );
                }
                else
                {
                    Console.WriteLine("The union of " + fileDialogIdPerm1.Access + " and " + 
                        fileDialogIdPerm2.Access + " = " + 
                        ((FileDialogPermission)fileDialogIdPerm3).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;
        FileDialogPermissionAccess fileDialog1,fileDialog2;
        FileDialogPermission fileDialogIdPerm1,fileDialogIdPerm2,fileDialogIdPerm3;
        FileDialogGenerator fileDialogGen1 = new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 = new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(out fileDialogIdPerm1, out fileDialog1)) 
        {
            if(fileDialogIdPerm1 == null) continue;
            Console.WriteLine("**********************************************************\n");
            fileDialogGen2.ResetIndex();

            while(fileDialogGen2.CreateFileDialog(out fileDialogIdPerm2, out fileDialog2)) 
            {
                if(fileDialogIdPerm2 == null) continue;
                //Console.WriteLine("\tfileDialogIdPerm2 = " +fileDialogIdPerm2.ToString());
    
                fileDialogIdPerm3 = (FileDialogPermission)fileDialogIdPerm1.Intersect(fileDialogIdPerm2);
                if (fileDialogIdPerm3 != null)
                {
                    Console.WriteLine("The intersection of " + fileDialog1 + " and " + fileDialog2 + " = " + 
                        ((FileDialogPermission)fileDialogIdPerm3).Access.ToString());
                }
                else
                {
                    Console.WriteLine("The intersection of " + fileDialog1 + " and " + fileDialog2 + " is null." );
                }
            }
        }

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

        bool returnValue = true;

        FileDialogPermissionAccess fileDialog1;
        FileDialogPermission fileDialogIdPerm1,fileDialogIdPerm2;

        FileDialogGenerator fileDialogGen1 = new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 = new FileDialogGenerator();

        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(out fileDialogIdPerm1, out fileDialog1)) 
        {
            if(fileDialogIdPerm1 == null) continue;
            fileDialogGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");
            try
            {            
                fileDialogIdPerm2 = (FileDialogPermission)fileDialogIdPerm1.Copy();
                Console.WriteLine("Result of copy = " + fileDialogIdPerm2.ToString());

            }
            catch(Exception e) 
            {
                Console.WriteLine("Copy failed :" + fileDialogIdPerm1.ToString() + e);
                continue;
            }
        }
        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;

        FileDialogPermissionAccess fileDialog1;
        FileDialogPermission fileDialogIdPerm1,fileDialogIdPerm2;
        FileDialogGenerator fileDialogGen1 = new FileDialogGenerator();
        FileDialogGenerator fileDialogGen2 = new FileDialogGenerator();
        fileDialogGen1.ResetIndex();
        while(fileDialogGen1.CreateFileDialog(out fileDialogIdPerm1, out fileDialog1)) 
        {
            if(fileDialogIdPerm1 == null) continue;
            Console.WriteLine("********************************************************\n");
            fileDialogGen2.ResetIndex();
            try
            {            
                fileDialogIdPerm2= new FileDialogPermission(PermissionState.None);
                fileDialogIdPerm2.FromXml(fileDialogIdPerm1.ToXml());
                Console.WriteLine("Result of ToFromXml = " +
                    fileDialogIdPerm2.ToString());
    
            }
            catch(Exception e) 
            {
                Console.WriteLine("ToFromXml failed :" + fileDialogIdPerm1.ToString() + e);
                continue;
            }
        }

        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 class generates FileDialogPermission objects.

internal  class FileDialogGenerator
{


    private FileDialogPermissionAccess[] myFileDialog = 
    {
        FileDialogPermissionAccess.None,
        FileDialogPermissionAccess.Open, 
        FileDialogPermissionAccess.OpenSave, 
        FileDialogPermissionAccess.Save};

    private int fileDialogIndex = 0;

    public FileDialogGenerator()
    {
        ResetIndex();
    }

    public void ResetIndex()
    {
        fileDialogIndex = 0;
    }
// Create a FileDialogPermission.
    public bool CreateFileDialog(out FileDialogPermission fileDialogPerm, 
        out FileDialogPermissionAccess fileDialog)
    {

        if(fileDialogIndex >= myFileDialog.Length) 
        {
            fileDialogPerm = new FileDialogPermission(PermissionState.None);
            fileDialog=FileDialogPermissionAccess.None;
            return false;
        }
        fileDialog = myFileDialog[fileDialogIndex++];
        try
        {
            fileDialogPerm = new FileDialogPermission(fileDialog);
            return true;
        } 
        catch(Exception e)
        {
            Console.WriteLine("Cannot create FileDialogPermission: " + fileDialog +" "+e);
            fileDialogPerm = new FileDialogPermission(PermissionState.None);
            fileDialog=FileDialogPermissionAccess.None;
            return true;
        }
    }
} // End of FileDialogGenerator.

[C++] 
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
// of the FileDialogPermission class.

#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
using namespace System;
using namespace System::Security; 
using namespace System::Security::Permissions; 

[assembly:CLSCompliant(true)];

// This class generates FileDialogPermission objects.

private __gc class FileDialogGenerator
{
private:
   FileDialogPermissionAccess myFileDialog[];
   int fileDialogIndex;

public:
   FileDialogGenerator()
   {
      ResetIndex();
      FileDialogPermissionAccess tempFileDialog[] = {
         FileDialogPermissionAccess::None,
         FileDialogPermissionAccess::Open, 
         FileDialogPermissionAccess::OpenSave, 
         FileDialogPermissionAccess::Save};
      myFileDialog = tempFileDialog;
   }

   void ResetIndex()
   {
      fileDialogIndex = 0;
   }
   // Create a FileDialogPermission.
   bool CreateFileDialog([Out]FileDialogPermission** fileDialogPerm, 
      [Out]FileDialogPermissionAccess* fileDialog)
   {

      if(fileDialogIndex >= myFileDialog->Length) 
      {
         *fileDialogPerm = new FileDialogPermission(PermissionState::None);
         *fileDialog=FileDialogPermissionAccess::None;
         return false;
      }
      *fileDialog = myFileDialog[fileDialogIndex++];
      try
      {
         *fileDialogPerm = new FileDialogPermission(*fileDialog);
         return true;
      } 
      catch(Exception* e)
      {
         Console::WriteLine(S"Cannot create FileDialogPermission: {0} {1}", __box(*fileDialog), e);
         *fileDialogPerm = new FileDialogPermission(PermissionState::None);
         *fileDialog=FileDialogPermissionAccess::None;
         return true;
      }
   }
}; // End of FileDialogGenerator.

public __gc class FileDialogPermissionDemo
{
   // IsSubsetOf determines whether the current permission is a subset of the 
   // specified permission.
private:
   bool IsSubsetOfDemo()
   {
      bool returnValue = true;
      FileDialogPermissionAccess fileDialog1,fileDialog2;
      FileDialogPermission* fileDialogIdPerm1;
      FileDialogPermission* fileDialogIdPerm2;

      FileDialogGenerator* fileDialogGen1 = new FileDialogGenerator();
      FileDialogGenerator* fileDialogGen2 = new FileDialogGenerator();
      fileDialogGen1->ResetIndex();
      while(fileDialogGen1->CreateFileDialog(&fileDialogIdPerm1, &fileDialog1)) 
      {
         if(fileDialogIdPerm1 == 0) continue;
         fileDialogGen2->ResetIndex();
         Console::WriteLine(S"********************************************************\n");

         while(fileDialogGen2->CreateFileDialog(&fileDialogIdPerm2, &fileDialog2)) 
         {
            if(fileDialogIdPerm2 == 0) continue;
            try
            {            
               if (fileDialogIdPerm1->IsSubsetOf(fileDialogIdPerm2))
               {
                  Console::WriteLine(S"{0} is a subset of {1}", __box(fileDialogIdPerm1->Access), __box(fileDialogIdPerm2->Access));
               }
               else
               {
                  Console::WriteLine(S"{0} is not a subset of {1}", __box(fileDialogIdPerm1->Access), __box(fileDialogIdPerm2->Access));
               }
            }
            catch(Exception* e) 
            {
               Console::WriteLine(S"An exception was thrown for subset :{0}\n{1}\n{2}", fileDialogIdPerm1, fileDialogIdPerm2, e);
               returnValue=false;
            }
         }
      }
      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;
      FileDialogPermissionAccess fileDialog1,fileDialog2;
      FileDialogPermission* fileDialogIdPerm1;
      FileDialogPermission* fileDialogIdPerm2;

      IPermission* fileDialogIdPerm3;
      FileDialogGenerator* fileDialogGen1 = new FileDialogGenerator();
      FileDialogGenerator* fileDialogGen2 = new FileDialogGenerator();
      fileDialogGen1->ResetIndex();
      while(fileDialogGen1->CreateFileDialog(&fileDialogIdPerm1, &fileDialog1)) 
      {
         if(fileDialogIdPerm1 == 0) continue;
         Console::WriteLine(S"**********************************************************\n");
         fileDialogGen2->ResetIndex();

         while(fileDialogGen2->CreateFileDialog(&fileDialogIdPerm2, &fileDialog2)) 
         {
            if(fileDialogIdPerm2 == 0) continue;
            fileDialogIdPerm3 = dynamic_cast<FileDialogPermission*>(fileDialogIdPerm1->Union(fileDialogIdPerm2));
            fileDialogIdPerm3 = fileDialogIdPerm1->Union(fileDialogIdPerm2);

            if(fileDialogIdPerm3 == 0)  
            {
               Console::WriteLine(S"The union of {0} and {1} is null.", __box(fileDialog1), __box(fileDialog2));
            }
            else
            {
               Console::WriteLine(S"The union of {0} and {1} = {2}", __box(fileDialogIdPerm1->Access), __box(fileDialogIdPerm2->Access), __box((dynamic_cast<FileDialogPermission*>(fileDialogIdPerm3))->Access));
            }

         }
      }

      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;
      FileDialogPermissionAccess fileDialog1,fileDialog2;
      FileDialogPermission* fileDialogIdPerm1;
      FileDialogPermission* fileDialogIdPerm2;
      FileDialogPermission* fileDialogIdPerm3;

      FileDialogGenerator* fileDialogGen1 = new FileDialogGenerator();
      FileDialogGenerator* fileDialogGen2 = new FileDialogGenerator();
      fileDialogGen1->ResetIndex();
      while(fileDialogGen1->CreateFileDialog(&fileDialogIdPerm1, &fileDialog1)) 
      {
         if(fileDialogIdPerm1 == 0) continue;
         Console::WriteLine(S"**********************************************************\n");
         fileDialogGen2->ResetIndex();

         while(fileDialogGen2->CreateFileDialog(&fileDialogIdPerm2, &fileDialog2)) 
         {
            if(fileDialogIdPerm2 == 0) continue;
            //Console.WriteLine("\tfileDialogIdPerm2 = " +fileDialogIdPerm2.ToString());

            fileDialogIdPerm3 = dynamic_cast<FileDialogPermission*>(fileDialogIdPerm1->Intersect(fileDialogIdPerm2));
            if (fileDialogIdPerm3 != 0)
            {
               Console::WriteLine(S"The intersection of {0} and {1} = {2}", __box(fileDialog1), __box(fileDialog2), __box((dynamic_cast<FileDialogPermission*>(fileDialogIdPerm3))->Access));
            }
            else
            {
               Console::WriteLine(S"The intersection of {0} and {1} is null.", __box(fileDialog1), __box(fileDialog2));
            }
         }
      }

      return returnValue;
   }
   // Copy creates and returns an identical copy of the current permission.
private:
   bool CopyDemo()
   {

      bool returnValue = true;

      FileDialogPermissionAccess fileDialog1;
      FileDialogPermission* fileDialogIdPerm1;
      FileDialogPermission* fileDialogIdPerm2;


      FileDialogGenerator* fileDialogGen1 = new FileDialogGenerator();
      FileDialogGenerator* fileDialogGen2 = new FileDialogGenerator();

      fileDialogGen1->ResetIndex();
      while(fileDialogGen1->CreateFileDialog(&fileDialogIdPerm1, &fileDialog1)) 
      {
         if(fileDialogIdPerm1 == 0) continue;
         fileDialogGen2->ResetIndex();
         Console::WriteLine(S"********************************************************\n");
         try
         {            
            fileDialogIdPerm2 = dynamic_cast<FileDialogPermission*>(fileDialogIdPerm1->Copy());
            Console::WriteLine(S"Result of copy = {0}", fileDialogIdPerm2);

         }
         catch(Exception* e) 
         {
            Console::WriteLine(S"Copy failed :{0}{1}", fileDialogIdPerm1, e);
            continue;
         }
      }
      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;

      FileDialogPermissionAccess fileDialog1;
      FileDialogPermission* fileDialogIdPerm1;
      FileDialogPermission* fileDialogIdPerm2;

      FileDialogGenerator* fileDialogGen1 = new FileDialogGenerator();
      FileDialogGenerator* fileDialogGen2 = new FileDialogGenerator();
      fileDialogGen1->ResetIndex();
      while(fileDialogGen1->CreateFileDialog(&fileDialogIdPerm1, &fileDialog1)) 
      {
         if(fileDialogIdPerm1 == 0) continue;
         Console::WriteLine(S"********************************************************\n");
         fileDialogGen2->ResetIndex();
         try
         {            
            fileDialogIdPerm2= new FileDialogPermission(PermissionState::None);
            fileDialogIdPerm2->FromXml(fileDialogIdPerm1->ToXml());
            Console::WriteLine(S"Result of ToFromXml = {0}", fileDialogIdPerm2);

         }
         catch(Exception* e) 
         {
            Console::WriteLine(S"ToFromXml failed :{0}{1}", fileDialogIdPerm1, e);
            continue;
         }
      }

      return returnValue;

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

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

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

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

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

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

      return (  returnCode );    

   }
};

// Test harness.
int main() 
{
   try
   {
      FileDialogPermissionDemo* democase = new FileDialogPermissionDemo();
      bool returnCode = democase->RunDemo();
      if (returnCode)
      {
         Console::WriteLine(S"FileDialogPermission demo completed successfully.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 100;
      }
      else
      {
         Console::WriteLine(S"FileDialogPermission demo failed.");
         Console::WriteLine(S"Press the Enter key to exit.");
         Console::ReadLine();
         System::Environment::ExitCode = 101;
      }
   }
   catch(Exception* e)
   {
      Console::WriteLine(S"FileDialogPermission failed.");
      Console::WriteLine(e);
      Console::WriteLine(S"Press the Enter key to exit.");
      Console::ReadLine();
      System::Environment::ExitCode = 101;
   }
}


[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Security.Permissions

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

FileDialogPermission Members | System.Security.Permissions Namespace | Permissions | Requesting Permissions | FileDialogPermissionAttribute | FileDialogPermissionAccess

Show:
© 2014 Microsoft