Export (0) Print
Expand All

FileDialogPermission Constructor (FileDialogPermissionAccess)

Initializes a new instance of the FileDialogPermission class with the specified access.

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

public FileDialogPermission (
	FileDialogPermissionAccess access
)
public FileDialogPermission (
	FileDialogPermissionAccess access
)
public function FileDialogPermission (
	access : FileDialogPermissionAccess
)

Parameters

access

A bitwise combination of the FileDialogPermissionAccess values.

Exception typeCondition

ArgumentException

The access parameter is not a valid combination of the FileDialogPermissionAccess values.

The following code example demonstrates the declarative and imperative forms of the FileDialogPermission class. The first attempt to display the dialog is successful; subsequent attempts fail due to the Deny issued from the Main method. Declarative security is used for the class to request minimum permissions for the code in the class to run.

using System;
using System.Security;
using System.Security.Permissions;
using System.Windows.Forms;
using System.IO;

// Add the declarative FileDialogPermissionAttribute to request minimum
// permissions for the code in this class to run.
[assembly:FileDialogPermissionAttribute(
    SecurityAction.RequestMinimum, 
    Open=true)]
public class DialogPermissions
{
    private Stream fileStream;

    public static void Main()
    {
        DialogPermissions dialogAccess = new DialogPermissions();
        Console.WriteLine("Opened file " + dialogAccess.OpenFile());

        // Deny permission to use of the Open dialog box.
        FileDialogPermission dialogPermission = 
            new FileDialogPermission(FileDialogPermissionAccess.Open);

        dialogPermission.Deny();

        dialogAccess.OpenFile();
        dialogAccess.CloseStream();

    }

    // Display open dialog box, store the file stream of the specified file
    // in the fileStream member variable.
    private string OpenFile()
    {
        string fileName = null;

        OpenFileDialog openDialogBox = new OpenFileDialog();
        openDialogBox.InitialDirectory = "c:\\";
        openDialogBox.Filter = "txt files (*.txt)|*.txt";
        openDialogBox.RestoreDirectory = true;
        try
        {
            // If user selects OK, store file name and stream.
            if(openDialogBox.ShowDialog() == DialogResult.OK)
            {
                fileName = openDialogBox.FileName;
                fileStream = openDialogBox.OpenFile();
                fileStream.Close();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        openDialogBox.Dispose();

        // Return specified file name.
        return fileName;
    }

    // Close the file stream.
    private void CloseStream()
    {
        if (fileStream != null)
        {
            fileStream.Close();
        }
    }
}
//
// This sample produces the following output:
//
// Opened file C:\testfile.txt
// Request for the permission of type 
// System.Security.Permissions.FileDialogPermission, mscorlib, 
// Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
// failed.
//
// This sample completed successfully; press Exit to continue.

import System.*;
import System.Security.*;
import System.Security.Permissions.*;
import System.Windows.Forms.*;
import System.IO.*;

// Add the declarative FileDialogPermissionAttribute to request minimum
// permissions for the code in this class to run.
/** @assembly FileDialogPermissionAttribute(SecurityAction.RequestMinimum,
    Open = true)
 */
public class DialogPermissions
{    
    private Stream fileStream;

    public static void main(String[] args)
    {
        DialogPermissions dialogAccess = new DialogPermissions();
        Console.WriteLine("Opened file " + dialogAccess.OpenFile());

        // Deny permission to use of the Open dialog box.
        FileDialogPermission dialogPermission = 
            new FileDialogPermission(FileDialogPermissionAccess.Open);
        dialogPermission.Deny();

        dialogAccess.OpenFile();
        dialogAccess.CloseStream();
    } //main

    // Display open dialog box, store the file stream of the specified file
    // in the fileStream member variable.
    private String OpenFile()
    {
        String fileName = null;

        OpenFileDialog openDialogBox = new OpenFileDialog();
        openDialogBox.set_InitialDirectory("c:\\");
        openDialogBox.set_Filter("txt files (*.txt)|*.txt");
        openDialogBox.set_RestoreDirectory(true);
        try {
            // If user selects OK, store file name and stream.
            if (openDialogBox.ShowDialog().Equals(DialogResult.OK)) {
                fileName = openDialogBox.get_FileName();
                fileStream = openDialogBox.OpenFile();
                fileStream.Close();
            }
        }
        catch (System.Exception ex) {
            Console.WriteLine(ex.get_Message());
        }
        openDialogBox.Dispose();
        // Return specified file name.
        return fileName;
    } //OpenFile

    // Close the file stream.
    private void CloseStream()
    {
        if (fileStream != null) {
            fileStream.Close();
        }
    } //CloseStream
} //DialogPermissions
//
// This sample produces the following output:
//
// Opened file C:\testfile.txt
// Request for the permission of type 
// System.Security.Permissions.FileDialogPermission, mscorlib, 
// Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
// failed.
//
// This sample completed successfully; press Exit to continue.

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