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)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class FileDialogPermission sealed : public 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

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.

using namespace System::Runtime::InteropServices;
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;

// This class generates FileDialogPermission objects.

[assembly:CLSCompliant(true)];
public ref class FileDialogPermissionDemo
{
private:

    // IsSubsetOf determines whether the current permission is a subset of the 
    // specified permission.
    bool IsSubsetOfDemo()
    {
        bool returnValue = true;
        FileDialogPermission^ fileDialogIdPerm1 = gcnew FileDialogPermission(FileDialogPermissionAccess::Open );
        FileDialogPermission^ fileDialogIdPerm2 = gcnew FileDialogPermission( FileDialogPermissionAccess::OpenSave );
        if ( fileDialogIdPerm1->IsSubsetOf( fileDialogIdPerm2 ) )
        {
            Console::WriteLine( "{0} is a subset of {1}", fileDialogIdPerm1->Access, fileDialogIdPerm2->Access );
        }
        else
        {
            Console::WriteLine( "{0} is not a subset of {1}", fileDialogIdPerm1->Access, fileDialogIdPerm2->Access );
        }

        return returnValue;
    }


    // Union creates a new permission that is the union of the current permission and the 
    // specified permission.
    bool UnionDemo()
    {
        bool returnValue = true;
        FileDialogPermission^ fileDialogIdPerm1 = gcnew FileDialogPermission(FileDialogPermissionAccess::Open );
        FileDialogPermission^ fileDialogIdPerm2 = gcnew FileDialogPermission( FileDialogPermissionAccess::OpenSave );
        FileDialogPermission^ fileDialogIdPerm3 = gcnew FileDialogPermission(PermissionState::None);
        fileDialogIdPerm3 = dynamic_cast<FileDialogPermission^>(fileDialogIdPerm1->Union( fileDialogIdPerm2 ));
        if ( fileDialogIdPerm3 == nullptr )
        {
            Console::WriteLine( "The union of {0} and {1} is null.", 
                fileDialogIdPerm1->Access, fileDialogIdPerm2->Access );
        }
        else
        {
            Console::WriteLine( "The union of {0} and {1} = {2}", 
                fileDialogIdPerm1->Access, fileDialogIdPerm2->Access, (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.
    bool IntersectDemo()
    {
        bool returnValue = true;
        FileDialogPermission^ fileDialogIdPerm1 = 
            gcnew FileDialogPermission(FileDialogPermissionAccess::Open );
        FileDialogPermission^ fileDialogIdPerm2 = 
            gcnew FileDialogPermission( FileDialogPermissionAccess::OpenSave );

        FileDialogPermission^ fileDialogIdPerm3 = 
            gcnew FileDialogPermission(PermissionState::None);

        //Console.WriteLine("\tfileDialogIdPerm2 = " +fileDialogIdPerm2.ToString());
        fileDialogIdPerm3 = dynamic_cast<FileDialogPermission^>(fileDialogIdPerm1->Intersect( fileDialogIdPerm2 ));
        if ( fileDialogIdPerm3 != nullptr )
        {
            Console::WriteLine( "The intersection of {0} and {1} = {2}", 
                fileDialogIdPerm1->Access, fileDialogIdPerm2->Access, (dynamic_cast<FileDialogPermission^>(fileDialogIdPerm3))->Access );
        }
        else
        {
            Console::WriteLine( "The intersection of {0} and {1} is null.", 
                fileDialogIdPerm1->Access, fileDialogIdPerm2->Access );
        }

        return returnValue;
    }


    // Copy creates and returns an identical copy of the current permission.
    bool CopyDemo()
    {
        bool returnValue = true;
        FileDialogPermission^ fileDialogIdPerm1 = gcnew FileDialogPermission(FileDialogPermissionAccess::Open );
        FileDialogPermission^ fileDialogIdPerm2 = gcnew FileDialogPermission( FileDialogPermissionAccess::OpenSave );

        FileDialogPermission^ fileDialogIdPerm3 = gcnew FileDialogPermission(PermissionState::None);

        fileDialogIdPerm2 = dynamic_cast<FileDialogPermission^>(fileDialogIdPerm1->Copy());
        Console::WriteLine( "Result of copy = {0}", fileDialogIdPerm2 );

        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. 
    bool ToFromXmlDemo()
    {
        bool returnValue = true;
        FileDialogPermission^ fileDialogIdPerm1 = gcnew FileDialogPermission(FileDialogPermissionAccess::Open );
        FileDialogPermission^ fileDialogIdPerm2 = gcnew FileDialogPermission( FileDialogPermissionAccess::OpenSave );

        FileDialogPermission^ fileDialogIdPerm3 = gcnew FileDialogPermission(PermissionState::None);

        fileDialogIdPerm2 = gcnew FileDialogPermission( PermissionState::None );
        fileDialogIdPerm2->FromXml( fileDialogIdPerm1->ToXml() );
        Console::WriteLine( "Result of ToFromXml = {0}", fileDialogIdPerm2 );

        return returnValue;
    }


public:

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

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

        returnCode = tempReturnCode && returnCode;

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

        returnCode = tempReturnCode && returnCode;

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

        returnCode = tempReturnCode && returnCode;

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

        returnCode = tempReturnCode && returnCode;

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

        returnCode = tempReturnCode && returnCode;
        return (returnCode);
    }

};


// Test harness.
int main()
{
    try
    {
        FileDialogPermissionDemo^ democase = gcnew FileDialogPermissionDemo;
        bool returnCode = democase->RunDemo();
        if ( returnCode )
        {
            Console::WriteLine( "FileDialogPermission demo completed successfully." );
            Console::WriteLine( "Press the Enter key to exit." );
            Console::ReadLine();
            System::Environment::ExitCode = 100;
        }
        else
        {
            Console::WriteLine( "FileDialogPermission demo failed." );
            Console::WriteLine( "Press the Enter key to exit." );
            Console::ReadLine();
            System::Environment::ExitCode = 101;
        }
    }
    catch ( Exception^ e ) 
    {
        Console::WriteLine( "FileDialogPermission failed." );
        Console::WriteLine( e );
        Console::WriteLine( "Press the Enter key to exit." );
        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 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:
© 2014 Microsoft