FileDialogPermissionAccess Enumeration
TOC
Collapse the table of content
Expand the table of content

FileDialogPermissionAccess Enumeration

Specifies the type of access to files allowed through the File dialog boxes.

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

[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum FileDialogPermissionAccess
/** @attribute SerializableAttribute() */ 
/** @attribute FlagsAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum FileDialogPermissionAccess
SerializableAttribute 
FlagsAttribute 
ComVisibleAttribute(true) 
public enum FileDialogPermissionAccess

 Member nameDescription
NoneNo access to files through the File dialog boxes. 
OpenAbility to open files through the File dialog boxes. 
OpenSaveAbility to open and save files through the File dialog boxes. 
SaveAbility to save files through the File dialog boxes. 

The following code example demonstrates the declarative and imperative forms of the FileDialogPermission class by using the FileDialogPermissionAccess enumeration to specify the type of access that is to be allowed through the File dialog boxes.

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft