FileDialogPermission Constructor (FileDialogPermissionAccess)
Collapse the table of content
Expand the table of content

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
)

Parameters

access
Type: System.Security.Permissions.FileDialogPermissionAccess

A bitwise combination of the FileDialogPermissionAccess values.

ExceptionCondition
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 box is successful; subsequent attempts fail because of the Deny issued from the Main method. The example uses declarative security 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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft